Results 1 to 1 of 1

Thread: QTreeWidget: Drag and Drop

  1. #1
    Join Date
    Aug 2009
    Posts
    23
    Thanks
    3

    Default QTreeWidget: Drag and Drop

    Hi everybody,

    I am currently trying to enable the Drag n Drop functionnality for a QTreeWidget.
    I am able to drag and drop items but I am clueless on two problems:
    1. I just want to move items around but it seems that the item is copied when I drag n drop. However the action is a Qt::MoveAction when I look at it in QTreeWidget::dropMimeData().
    2. I am able to drop an item onto another item which I don't want. I use a custom QTreeWidgetItem without the flag Qt::ItemIsDropEnabled. It works for the first drag n drop action, but not anymore on the 2nd action.


    Here is my code:

    Qt Code:
    1. QMyTreeItem::QMyTreeItem(QTreeWidgetItem* parent, const boost::shared_ptr<Filter>& filter) : QTreeWidgetItem(parent), m_filter(filter) {
    2.  
    3. setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
    4. setText(QFilterTree::UNIQUE_COLUMN_INDEX, filter->getName().toStdString().c_str());
    5. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QMyTree::QMyTree(QWidget *parent) : QTreeWidget(parent) {
    2. setColumnCount(1);
    3. header()->hide();
    4. setSelectionBehavior(QAbstractItemView::SelectRows); // Only rows are selectable
    5. setSelectionMode(QAbstractItemView::SingleSelection); // Define selection behavior
    6. setDragEnabled(true);
    7. viewport()->setAcceptDrops(true);
    8. setDropIndicatorShown(true);
    9. setDragDropMode(QAbstractItemView::InternalMove);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void QMyTree::refresh() {
    2. FilterList filters = DsmEditorWorkbench::getInstance()->getDsmProjectManager()->getActiveProject()->getEditedDsm()->getFilters();
    3. for (FilterList::const_iterator it = filters.begin(); it != filters.end() ; it++) {
    4. FilterShPtr filter = *it;
    5. QMyTreeItem* treeItem = new QMyTreeItem(this, filter);
    6. addTopLevelItem(treeItem);
    7. }
    8.  
    9. for (int i=0; i <topLevelItemCount(); i++) {
    10. QTreeWidgetItem* item = topLevelItem(i);
    11. if (item) {
    12. qDebug() << "QMyTree::refresh() => item present at index: " << i;
    13.  
    14. if (dynamic_cast <QMyTreeItem*> (item)) {
    15. qDebug() << "QMyTree::refresh() => It's a QMyTreeItem";
    16. }
    17. }
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void QMyTree::dropEvent(QDropEvent* event) {
    2. qDebug() << "QMyTree::dropEvent()";
    3. QAbstractItemView::dropEvent(event);
    4.  
    5. // Look over top level items
    6. for (int i=0; i <topLevelItemCount(); i++) {
    7. QTreeWidgetItem* item = topLevelItem(i);
    8. if (item) {
    9. qDebug() << "QMyTree::dropEvent() => item present at index: " << i;
    10.  
    11. if (dynamic_cast <QMyTreeItem*> (item)) {
    12. qDebug() << "QMyTree::dropEvent() => It's a QMyTreeItem";
    13. }
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    In the example below, I'll have 2 items:
    |-item1
    |
    |-item2

    Here is the messages I get after the refresh() method when opening the application:
    QMyTree::refresh()
    QMyTree::refresh() => item present at index: 0
    QMyTree::refresh() => It's a QMyTreeItem
    QMyTree::refresh() => item present at index: 1
    QMyTree::refresh() => It's a QMyTreeItem

    1rst drag n drop => I put item1 below item2:
    |-item2
    |
    |-item1

    Messages:
    QMyTree::dropEvent()
    QMyTree::dropEvent() => item present at index: 0
    QMyTree::dropEvent() => It's a QMyTreeItem
    QMyTree::dropEvent() => item present at index: 1
    QMyTree::dropEvent() => It's a QMyTreeItem
    QMyTree::dropEvent() => item present at index: 2

    2nd drag n drop => I put item2 below item1 (initial order):
    |-item1
    |
    |-item2

    Messages:
    QMyTree::dropEvent()
    QMyTree::dropEvent() => item present at index: 0
    QMyTree::dropEvent() => It's a QMyTreeItem
    QMyTree::dropEvent() => item present at index: 1
    QMyTree::dropEvent() => item present at index: 2


    It looks like my custom QMyTreeItem(s) are replaced by QTreeWidgetItem(s).
    Anybody who have a better understanding can help me please?

    I am also wondering if solving the 1rst issue would solve the 2nd issue.

    Best Regards
    Lionel
    Last edited by laugusti; 11th June 2010 at 15:52.

Similar Threads

  1. QTreeView: Drag and Drop
    By laugusti in forum Newbie
    Replies: 7
    Last Post: 19th March 2010, 19:05
  2. QTreeView Drag and Drop with MyData
    By baray98 in forum Qt Programming
    Replies: 4
    Last Post: 20th August 2009, 17:56
  3. Drag and drop in QTreeView
    By Valheru in forum Qt Programming
    Replies: 3
    Last Post: 27th July 2008, 09:36
  4. drag and drop from QTreeView
    By Untersander in forum Qt Programming
    Replies: 1
    Last Post: 10th April 2006, 09:00
  5. Drag & drop for QTreeView
    By yogeshm02 in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2006, 14:32

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.