Results 1 to 4 of 4

Thread: Drag 'n Drop QListWidget: no text while dragging if not default drag used

  1. #1
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Drag 'n Drop QListWidget: no text while dragging if not default drag used

    I have a little problem with DnD using a QListWidget.
    I have to create my own QListWidget class (called CListWidget) as I need to implement my own mousePressEvent method.

    Qt Code:
    1. void CListWidget::mousePressEvent(QMouseEvent *event)
    2. {
    3. QListWidget::mousePressEvent(event);
    4.  
    5. if ( currentItem()->text().isEmpty() )
    6. {
    7. clearSelection();
    8. return;
    9. }
    10.  
    11. QByteArray itemData;
    12. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    13. dataStream << currentItem()->text();
    14.  
    15. QMimeData *mimeData = new QMimeData;
    16. mimeData->setData("application/x-dnditemdata", itemData);
    17. mimeData->setText(currentItem()->text());
    18.  
    19. QDrag *drag = new QDrag(this);
    20. drag->setMimeData(mimeData);
    21. drag->exec(Qt::CopyAction);
    22. }
    To copy to clipboard, switch view to plain text mode 

    When using this code, I cannot see any text while dragging.
    If I comment the code and just enable the drag:

    Qt Code:
    1. CListWidget::CListWidget(QWidget* parent) : QListWidget(parent)
    2. {
    3. setDragEnabled(true);
    4. }
    To copy to clipboard, switch view to plain text mode 

    the text of the CListWidget item is shown while dragging.

    Why ?

    The problem is also that, if I use the QT default drag, I don't have any text in the item dragged.

    Thanks for any suggestion


    PS: Using Qt version 4.4.0 under Linux Ubuntu 8.04

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag 'n Drop QListWidget: no text while dragging if not default drag used

    You will need to set pixmap in mime data to see the text while dragging...
    From the docs -
    Qt Code:
    1. void QAbstractItemView::startDrag(Qt::DropActions supportedActions)
    2. {
    3. QModelIndexList indexes = d->selectedDraggableIndexes();
    4. if (indexes.count() > 0) {
    5. QMimeData *data = d->model->mimeData(indexes);
    6. if (!data)
    7. return;
    8. QRect rect;
    9. QPixmap pixmap = d->renderToPixmap(indexes, &rect); // HERE THEY RENDER TEXT TO PIXMAP
    10. rect.adjust(horizontalOffset(), verticalOffset(), 0, 0);
    11. QDrag *drag = new QDrag(this);
    12. drag->setPixmap(pixmap); // THE PIXMAP FOR SHOWING TEXT
    13. drag->setMimeData(data);
    14. drag->setHotSpot(d->pressedPosition - rect.topLeft());
    15. Qt::DropAction defaultDropAction = Qt::IgnoreAction;
    16. if (d->defaultDropAction != Qt::IgnoreAction && (supportedActions & d->defaultDropAction))
    17. defaultDropAction = d->defaultDropAction;
    18. else if (supportedActions & Qt::CopyAction && dragDropMode() != QAbstractItemView::InternalMove)
    19. defaultDropAction = Qt::CopyAction;
    20. if (drag->exec(supportedActions, defaultDropAction) == Qt::MoveAction)
    21. d->clearOrRemove();
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag 'n Drop QListWidget: no text while dragging if not default drag used

    Thanks but I don't follow you...
    I guess I have to reimplement the virtual method startDrag in my CListWidget class, but I get errors.

  4. #4
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag 'n Drop QListWidget: no text while dragging if not default drag used

    Well, I've solved in this way:

    added the following to the constructor:

    Qt Code:
    1. QPen pen(QColor("blue"));
    2. m_Pixmap = new QPixmap(QSize(180, 20));
    3. m_Painter = new QPainter(m_Pixmap);
    4. m_Painter->setPen(pen);
    To copy to clipboard, switch view to plain text mode 

    and the mousePressEvent is now:

    Qt Code:
    1. void CListWidget::mousePressEvent(QMouseEvent *event)
    2. {
    3. QListWidget::mousePressEvent(event);
    4.  
    5. if ( currentItem()->text().isEmpty() )
    6. {
    7. clearSelection();
    8. return;
    9. }
    10.  
    11. QByteArray itemData;
    12. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    13. dataStream << currentItem()->text();
    14.  
    15. m_Painter->fillRect(QRect(0, 0, 180, 20), QBrush(QColor("orange"))); // ADDED
    16. m_Painter->drawText(QRect(10, 0, 170, 20), currentItem()->text() ); // ADDED
    17.  
    18. QMimeData *mimeData = new QMimeData;
    19. mimeData->setData("application/x-dnditemdata", itemData);
    20. mimeData->setText(currentItem()->text());
    21.  
    22. QDrag *drag = new QDrag(this);
    23. drag->setPixmap(*m_Pixmap); // ADDED
    24. drag->setMimeData(mimeData);
    25. drag->exec(Qt::CopyAction);
    26. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Drag and drop between QListWidget's
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2011, 05:29
  2. QListWidget Drag and Drop problem
    By winkle99 in forum Qt Programming
    Replies: 0
    Last Post: 20th October 2009, 21:00
  3. QListWidget/QTreeWidget Drag and Drop
    By hlvietlong in forum Qt Programming
    Replies: 2
    Last Post: 30th June 2009, 19:09
  4. Drag from QTreeWidget and drop in QListWidget
    By WXNSNW in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2008, 01:15
  5. Model Drag'n'Drop - Default Move
    By Lemming in forum Qt Programming
    Replies: 2
    Last Post: 18th December 2006, 22:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.