Results 1 to 5 of 5

Thread: Drag and Drop inside QTableView with QStandardItemModel

  1. #1
    Join Date
    Jun 2012
    Posts
    38
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Drag and Drop inside QTableView with QStandardItemModel

    Dear ALl,

    I have a question that I am hoping someone in the Forum can help me with.

    I have QtableView, using the QStandardItemModel. I need to be able to sort/arrange the content of the model by dragging and dropping its items. Is this possible with the QStandarItemModel and QTableView?
    so in short I would be able to drag item2 and drop it on top of item1 - result should be that item1 and item2 swap place. I do not want to drop between, only on top of other items. No new lines should be created....

    I am configuring the QTableView the following way.


    Qt Code:
    1. ...
    2. tableView->setContextMenuPolicy(Qt::CustomContextMenu);
    3. tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
    4. tableView->setAlternatingRowColors(true);
    5. tableView->setDropIndicatorShown(true);
    6. tableView->setDragDropOverwriteMode(false);
    7. tableView->setDragEnabled(true);
    8. tableView->setDragDropMode(QAbstractItemView::InternalMove);
    9. tableView->setDragDropMode(QAbstractItemView::DragDrop);
    10. ...
    To copy to clipboard, switch view to plain text mode 


    BUt if I drag item1 and drop on item2 nothing happens. If I drag item1 and drop it between item2 and item3. item1 is appended to the model at the very bottom.??

    Any help is most appreciated.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Drag and Drop inside QTableView with QStandardItemModel

    tableView->setDragDropMode(QAbstractItemView:: InternalMove);
    tableView->setDragDropMode(QAbstractItemView:: DragDrop);
    The second line cancels the first; it isn't an "OR" operation.

    Have you read this?

  3. The following user says thank you to d_stranz for this useful post:

    gig-raf (17th February 2016)

  4. #3
    Join Date
    Jun 2012
    Posts
    38
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and Drop inside QTableView with QStandardItemModel

    Thanks for taking the time to answer me. True with the bottom two lines. I removed the bottom one, and left solely the InternalMove.

    I can now drop item1 on item2, but then item1 is cleared (removed) and item2 remains unchanged. What I want is that item1 and item2 just swap place.

    I will have a read at you link and see if I can progress with this tomorrow.

    cheers!

  5. #4
    Join Date
    Jun 2012
    Posts
    38
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag and Drop inside QTableView with QStandardItemModel

    Okay managed to get it working with QTableView and QStandardItems.

    here for those who might have similar need:

    The key for the success was setting the item.setFlags as below and the three drag options at the bottom.


    Qt Code:
    1. model = new QStandardItemModel(0,1,this);
    2.  
    3. // Attach the model to the view
    4. ui->downloadView->setModel(model);
    5. QList<QStandardItem*> data;
    6. itm = new QStandardItem("Breitner");
    7. itm->setFlags(itm->flags() & ~(Qt::ItemIsDropEnabled));
    8. data.append(itm);
    9. model->appendRow(data);
    10. data.clear();
    11. itm = new QStandardItem("Laudrup");
    12. itm->setFlags(itm->flags() & ~(Qt::ItemIsDropEnabled));
    13. data.append(itm);
    14. model->appendRow(data);
    15. data.clear();
    16. itm = new QStandardItem("Platini");
    17. itm->setFlags(itm->flags() & ~(Qt::ItemIsDropEnabled));
    18. data.append(itm);
    19. model->appendRow(data);
    20. data.clear();
    21. itm = new QStandardItem("Paolo Rossi");
    22. itm->setFlags(itm->flags() & ~(Qt::ItemIsDropEnabled));
    23. data.append(itm);
    24. model->appendRow(data);
    25. data.clear();
    26. itm = new QStandardItem("Lineker");
    27. itm->setFlags(itm->flags() & ~(Qt::ItemIsDropEnabled));
    28. data.append(itm);
    29. model->appendRow(data);
    30. data.clear();
    31.  
    32. ui->downloadView->setDragDropOverwriteMode(false);
    33. ui->downloadView->setDragEnabled(true);
    34. ui->downloadView->setDragDropMode(QAbstractItemView::InternalMove);
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to gig-raf for this useful post:

    d_stranz (27th January 2016)

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Drag and Drop inside QTableView with QStandardItemModel

    QStandardItem* itm = new QStandardItem();
    QList<QStandardItem*> data;
    itm = new QStandardItem("Breitner");
    The first line above is a memory leak because you create the item, do nothing with it, and then overwrite the pointer in the third line.

    Thanks for sharing your solution.

  8. The following user says thank you to d_stranz for this useful post:

    gig-raf (17th February 2016)

Similar Threads

  1. QStandardItemModel drag and drop
    By justin123 in forum Qt Programming
    Replies: 0
    Last Post: 24th May 2012, 04:08
  2. Replies: 2
    Last Post: 13th October 2010, 21:51
  3. Drag and drop inside and outside a Widget
    By utopia500 in forum Qt Programming
    Replies: 1
    Last Post: 4th May 2010, 16:20
  4. QStandardItemModel Drag n Drop Behavior with QTreeView
    By johnmidd in forum Qt Programming
    Replies: 0
    Last Post: 5th November 2009, 19:53
  5. Drag&Drop with QStandardItemModel
    By madcat in forum Qt Programming
    Replies: 4
    Last Post: 7th May 2006, 15:43

Tags for this Thread

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.