Results 1 to 6 of 6

Thread: QTReeWidget Drag and Drop

  1. #1
    Join Date
    Feb 2009
    Posts
    189
    Thanks
    2

    Default QTReeWidget Drag and Drop

    Dear Friends
    I am trying to implement drag and drop for QTreeWidgetItem.I have some toplevelitems and some of them have children items. I want to implement hte drag and drop functionality for the toplevel items, when an item if dragged and dropped the toplevele items and its children items all should occupy the new place. Somekind of reordering I want to achieve. Please give me some guidelines.
    I am curreny trying to implement startDrag() but its crashing.

    void Tree::startDrag(Qt:ropActions supportedActions)
    {
    QDrag *drag = new QDrag(this);
    QMimeData *data = new QMimeData;
    data->setText(currentItem()->text(0));
    drag->setMimeData(data);
    drag->exec(supportedActions, Qt::MoveAction);
    }

    Thanks in advance

  2. #2
    Join Date
    Feb 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTReeWidget Drag and Drop

    I've tried to do something like that recently. In mousePressEvent() function execute a new drag object and
    in dropEvent() function handle with replacing the item.
    Following code may help.

    Qt Code:
    1. void Tree::dragMoveEvent(QDragMoveEvent * event) {
    2. event->acceptProposedAction();
    3. }
    4.  
    5. void Tree::dragEnterEvent(QDragEnterEvent * event ) {
    6. event->acceptProposedAction();
    7.  
    8. }
    9.  
    10. void Tree::dropEvent(QDropEvent * event ) {
    11. dropingOn = this->itemAt(event->pos());
    12. int dropingIndex = this->indexOfTopLevelItem(dropingOn);
    13. this->takeTopLevelItem(this->indexOfTopLevelItem(draggingItem));
    14. int index = this->indexOfTopLevelItem(dropingOn);
    15. if(index < dropingIndex) index++;
    16. this->insertTopLevelItem(index, draggingItem);
    17.  
    18. }
    19.  
    20. void Tree::mousePressEvent(QMouseEvent *event) {
    21. if(event->button()==Qt::LeftButton) {
    22. draggingItem = this->itemAt(event->pos());
    23. QDrag *drag = new QDrag(this);
    24. drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction);
    25. event->accept();
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2009
    Posts
    189
    Thanks
    2

    Default Re: QTReeWidget Drag and Drop

    hi camelsoft your suggestion works fine but I am getting one problem, that I am not able to selecte any item in the treeWidget. it seems its disabled. what can be done for that. Thanks sujan


    Added after 30 minutes:


    Dear Friends
    I am implementing drag and drop for treeWidget. I am able to do it. But as I implement mousePressEvent() I am not able to expand or select items from the treeview as I have childitems for every toplevelitem.
    Whats going wrong in this. Please tell me someone.


    QTreeWidgetItem * draggingItem = new QTreeWidgetItem(this);

    void Tree::mousePressEvent(QMouseEvent *event)
    {
    if(event->button()==Qt::LeftButton)
    {
    draggingItem = this->itemAt(event->pos());
    QDrag *drag = new QDrag(this);
    drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction);
    event->accept();
    }
    }
    Last edited by sujan.dasmahapatra; 21st March 2011 at 09:00.

  4. #4
    Join Date
    Feb 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTReeWidget Drag and Drop

    in the constructor of your tree widget implementation add these lines:
    Qt Code:
    1. this->setDragDropMode(QAbstractItemView::InternalMove);
    2. this->setSelectionMode(QAbstractItemView::SingleSelection);
    3. this->setDragEnabled(true);
    4. this->setAcceptDrops(true);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2009
    Posts
    189
    Thanks
    2

    Default Re: QTReeWidget Drag and Drop

    Hi camelsoft,
    mousePressEvent is actually hindering QTreeWidget's normal functions, like selecvt an item, expand child indicator etc, so plss suggest some other way of dragging an item, like can aI use startDrag() instead of mousPressEvent... Thanks sujan

  6. #6
    Join Date
    Apr 2013
    Posts
    9
    Qt products
    Qt4 Qt5

    Default Re: QTReeWidget Drag and Drop

    Hii

    If the Source and Destniation TreeWidgets are two seperate classes, How drag and drop will achieve.. Any sample, please upload

    Sree

Similar Threads

  1. QTreeWidget Drag and drop
    By zell99 in forum Newbie
    Replies: 15
    Last Post: 7th August 2010, 14:28
  2. QTreeWidget: Drag and Drop
    By laugusti in forum Qt Programming
    Replies: 0
    Last Post: 11th June 2010, 16:37
  3. QtreeWidget drag and drop
    By addu in forum Qt Programming
    Replies: 4
    Last Post: 30th June 2009, 10:41
  4. QTreeWidget Drag and Drop
    By tommydent in forum Qt Programming
    Replies: 10
    Last Post: 25th August 2008, 16:25
  5. QTreeWidget drag/drop
    By s_a_white in forum Newbie
    Replies: 1
    Last Post: 10th February 2007, 23:04

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.