Results 1 to 6 of 6

Thread: Moving vs. copying in drag and drop

  1. #1
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Moving vs. copying in drag and drop

    I'm trying to learn how to do drag and drop. I wrote a simple program which is supposed to permit the user to sort items in a table. It almost does what I want, but when the user drops items at new locations, copies are made. I can't figure out how to change it so that the original item is deleted in addition to the copy being made. Can anyone offer me some assistance? Thanks!

    Qt Code:
    1. QApplication app(argc, argv);
    2.  
    3. QTableWidget table(5, 1);
    4. table.horizontalHeader()->setStretchLastSection(true);
    5. table.setHorizontalHeaderLabels(QStringList() << "Sort Items by Dragging");
    6.  
    7. table.setSelectionMode(QAbstractItemView::SingleSelection);
    8. table.setDragEnabled(true);
    9. table.setAcceptDrops(true);
    10. table.setDropIndicatorShown(true);
    11.  
    12. const char * labels[] = { "Item D", "Item B", "Item C", "Item E", "Item A" };
    13. for (int i=0; i < 5; ++i)
    14. {
    15. QTableWidgetItem * item = new QTableWidgetItem(labels[i]);
    16. item->setFlags(item->flags() & ~Qt::ItemIsDropEnabled);
    17. table.setItem(0, i, item);
    18. }
    19.  
    20. table.show();
    21. return app.exec();
    To copy to clipboard, switch view to plain text mode 
    - Lana

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Moving vs. copying in drag and drop

    Here you go: link

  3. #3
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Moving vs. copying in drag and drop

    Thanks for the reply. I tried setting the dragDropMode to QAbstractItemView::InternalMove but that also is not quite what I want.

    In the default state, copies of the item are created but the original is left behind.

    With InternalMove set, the item moves but an empty slot is left behind.

    I would like the item to move but no empty slot to be left behind. None of the other drag/drop related options I saw seemed to do just this, either.
    - Lana

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Moving vs. copying in drag and drop

    I couldn't get it to work either. "InternalMove" works fine on a treewidget which is where I have used it:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char * argv[]){
    4.  
    5. QApplication app(argc, argv);
    6.  
    7. tree.setHeaderLabel("Sort Items by Dragging");
    8. tree.setSelectionMode(QAbstractItemView::SingleSelection);
    9. tree.setDragEnabled(true);
    10. tree.setDropIndicatorShown(true);
    11. tree.viewport()->setAcceptDrops(true);
    12. tree.setDragDropMode(QAbstractItemView::InternalMove);
    13. QStringList labels;
    14. labels <<"Item D"<< "Item B"<< "Item C"<< "Item E"<< "Item A";
    15. for (int i=0; i < 5; ++i)
    16. {
    17. item->setText(0,labels[i]);
    18. item->setFlags(item->flags() & ~Qt::ItemIsDropEnabled);
    19. tree.insertTopLevelItem(i,item);
    20. }
    21. tree.show();
    22. return app.exec();
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 
    Probably not the look you want though ...

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Moving vs. copying in drag and drop

    You could allow the object to be moved and then when the object has been dropped remove the empty slot yourself. If you have created an appropriate model and data store for the objects, then this should be very easy to do and just require an update to be sent to the control.

  6. #6
    Join Date
    Jul 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Moving vs. copying in drag and drop

    Norobro, the Tree version works for me too. I was thinking Table but maybe a Tree view would be a natural model also. I will think about it. Thanks.

    Fatjuicymole, I took your suggestion and turned my Table into a derived class, then overrode the dropMimeData method so it called the base version then cleaned up the extra row. That makes the Table version work as desired. Thanks.
    - Lana

Similar Threads

  1. Replies: 3
    Last Post: 10th June 2010, 15:13
  2. Replies: 0
    Last Post: 4th May 2010, 10:24
  3. whole row drag/drop
    By gyre in forum Newbie
    Replies: 13
    Last Post: 27th November 2007, 08:56
  4. The drag and moving problem??
    By Ray in forum Qt Programming
    Replies: 10
    Last Post: 28th September 2006, 09:57
  5. Drag and Drop (drop example)
    By din9 in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 18:03

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.