Results 1 to 6 of 6

Thread: Drag and drop with QTreeWidget

  1. #1

    Default Drag and drop with QTreeWidget

    Hi,

    I subclassed the code below from QTreeWidget to enable drag and drop. The problem is when i'm draging an item containing childrens, the childrens don't follow the parent on the drop. Is there a problem with mimeData?

    Also, is there a way to obtain the index of the parent where I make the drop?

    Thanks
    Qt Code:
    1. void QMyTreeWidget::mouseMoveEvent(QMouseEvent *event)
    2. {
    3. QModelIndexList liste=selectedIndexes();
    4. QMimeData *mimeData = model()->mimeData(liste);
    5. QDrag *drag = new QDrag(this);
    6. drag->setMimeData(mimeData);
    7. drag->setHotSpot(event->pos() - rect().topLeft());
    8. Qt::DropAction dropAction = drag->start( Qt::MoveAction);
    9. if (dropAction == Qt::MoveAction) {
    10. model()->removeRows(liste[0].row(),liste.size(),liste[0].parent());
    11. }
    12. }
    13.  
    14.  
    15. void QMyTreeWidget::dropEvent(QDropEvent *e)
    16. {
    17. QTreeWidget::dropEvent(e);
    18. e->setDropAction(Qt::MoveAction);
    19. e->accept();
    20. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 6th February 2007 at 21:51. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drag and drop with QTreeWidget

    Is there any valid reason for implementing custom drag'n'drop? QTreeWidget supports drag'n'drop out of the box: Using Drag and Drop with Item Views - Using Convenience Views
    J-P Nurmi

  3. #3

    Default Re: Drag and drop with QTreeWidget

    Yes, I want to use the move action without using the atl key. The default drag and drop action is copy.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and drop with QTreeWidget

    Your drop doesn't handle any moving. First you call the base class and then when the event is already handled you change its type.

  5. #5

    Default Re: Drag and drop with QTreeWidget

    Ok, so what is the good way to do it? Any example?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and drop with QTreeWidget

    The best way to do it is to move the items yourself, if you have to reimplement the handler... If the default handler doesn't include children then you either need to change the drag to include children or change the drop to include children. Changing the action itself isn't enough, it'll just make the source remove the item it had placed into the drag (without children).

Similar Threads

  1. Drag & drop with model/view (Qt4)
    By yogeshm02 in forum Qt Programming
    Replies: 16
    Last Post: 19th September 2011, 20:36
  2. Drag and Drop
    By allensr in forum Qt Programming
    Replies: 1
    Last Post: 11th December 2006, 20:50
  3. Replies: 7
    Last Post: 8th September 2006, 16:19
  4. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 16:41
  5. Drag 'n Drop problem
    By kiker99 in forum Qt Programming
    Replies: 4
    Last Post: 16th January 2006, 16:35

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.