Results 1 to 11 of 11

Thread: Problem with QListWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2009
    Location
    Switzerland
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with QListWidget

    So, finally I've do my first idea. Here it's the code, it could help someone one day!

    Qt Code:
    1. void DragNDropQListWidget::startDrag(Qt::DropActions) {
    2. int row = currentRow();
    3.  
    4. QByteArray itemData;
    5. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    6.  
    7. //We put the current row into datastream
    8. dataStream << row;
    9.  
    10. QMimeData *mimeData = new QMimeData;
    11. mimeData->setData("application/x-dndgroup", itemData);
    12.  
    13. QDrag *drag = new QDrag(this);
    14. drag->setMimeData(mimeData);
    15.  
    16. drag->exec(Qt::MoveAction);
    17. }
    18.  
    19. void DragNDropQListWidget::dropEvent(QDropEvent *event) {
    20. if (event->mimeData()->hasFormat("application/x-dndgroup")) {
    21. QListWidgetItem *itemToDrag;
    22. QByteArray itemData = event->mimeData()->data("application/x-dndgroup");
    23. QDataStream dataStream(&itemData, QIODevice::ReadOnly);
    24. int previousRow;
    25.  
    26. //Will stock the target destination row
    27. int currentDropRow = row(itemAt(event->pos()));
    28.  
    29. //We take the source row
    30. dataStream >> previousRow;
    31.  
    32. //Beforhand, we've stock the itemWidget into a QList of QWidget*
    33. QVBoxLayout *vLayoutA = refWidget->at(previousRow);
    34. QWidget *wA = new QWidget;
    35. wA->setLayout(vLayoutA);
    36.  
    37.  
    38. QVBoxLayout *vLayoutTemp = refWidget->takeAt(previousRow);
    39. refWidget->insert(currentDropRow, vLayoutTemp);
    40.  
    41. //We "remove" the item of the list
    42. itemToDrag = takeItem(previousRow);
    43. //Important, we have to remove the item widget, otherwise it will be blank widget.
    44. removeItemWidget(itemToDrag);
    45. //We insert the removed item into the destination
    46. insertItem(currentDropRow, itemToDrag);
    47. //And we reset the item widget.
    48. setItemWidget(itemToDrag, wA);
    49.  
    50. setCurrentItem(itemToDrag);
    51.  
    52. if (event->source() == this) {
    53. event->setDropAction(Qt::MoveAction);
    54. event->accept();
    55. } else {
    56. event->ignore();
    57. }
    58. } else {
    59. event->ignore();
    60. }
    61. }
    To copy to clipboard, switch view to plain text mode 

    For buttons who up and down items, use the same idea (save the widget created into a QList).

  2. The following user says thank you to Elwe for this useful post:

    nabroyan (2nd November 2017)

Similar Threads

  1. Problem with QListWidget and paintEvent()
    By joksi in forum Qt Programming
    Replies: 2
    Last Post: 29th May 2009, 10:50
  2. QListWidget problem [solved]
    By satoshi in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2009, 18:58
  3. Replies: 1
    Last Post: 23rd April 2009, 09:05
  4. Replies: 19
    Last Post: 3rd April 2009, 23:17
  5. QListWidget - stylesheet problem
    By febil in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2009, 16:44

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
  •  
Qt is a trademark of The Qt Company.