Results 1 to 7 of 7

Thread: QTreeWidgetItem mime type

  1. #1
    Join Date
    Jul 2007
    Location
    New York
    Posts
    45
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default QTreeWidgetItem mime type

    I want to set the mime information for a QTreeWidgetItem so that drag and drop will properly work. Do I have to subclass treewidget or treewidgetitem or can I just set something somewhere in the tree items before they are dragged (like when they are created)?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeWidgetItem mime type

    See the examples here: http://doc.trolltech.com/4.3/dnd.html#dragging.

    Regards

  3. #3
    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: QTreeWidgetItem mime type

    You might want to take a look at QTreeWidget::mimeData() and QTreeWidget::dropMimeData().
    J-P Nurmi

  4. #4
    Join Date
    Jul 2007
    Location
    New York
    Posts
    45
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QTreeWidgetItem mime type

    I see how it is possible to subclass... this is well documented.

    I really just want to drop the first (and only) column from the treewidgetitem onto the target (perhaps as mime text/plaintext)... I thought there may be a simple way or it was already implemented and I was missing it.

    If I create a QTreeWidget and populate it with QTreeWidgetItems (not subclassing either one) then when I drag from the treewidget to somethere else the mime seems to be empty. Perhaps the text is hidden somewhere in the mimeData? It seems odd that there is nothing there, because the documentation seems to imply that D&D within the tree widget would work without subclassing if the right flags are turned on, which implies that TreeWidgetItem mime data has some default implementation.

    Perhaps I have to populate the treewidgetitem with data of a different role? The one I am using is Qt::DisplayRole.

  5. #5
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeWidgetItem mime type

    "By default, the built-in models and views use an internal MIME type (application/x-qabstractitemmodeldatalist) to pass around information about model indexes"

    Have you tried something like the following:

    Qt Code:
    1. void dropEvent( QGraphicsSceneDragDropEvent* event )
    2. {
    3. if (event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
    4. {
    5. QTreeWidget *tree = dynamic_cast<QTreeWidget *>(event->source());
    6.  
    7. QByteArray itemData = event->mimeData()->data("application/x-qabstractitemmodeldatalist");
    8. QDataStream stream(&itemData, QIODevice::ReadOnly);
    9.  
    10. int r, c;
    11. QMap<int, QVariant> v;
    12. stream >> r >> c >> v;
    13.  
    14. QTreeWidgetItem *item = tree->topLevelItem(r);
    15.  
    16. if( item )
    17. {
    18. itemDropped(item);
    19. }
    20. }
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Sep 2007
    Posts
    29
    Thanks
    8

    Default Re: QTreeWidgetItem mime type

    does the standard mime-type actually contain information on sub-items etc.?
    it seems when using drag 'n drop with trees, the structure of the items gets flattened to a list.

  7. #7
    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: QTreeWidgetItem mime type

    Quote Originally Posted by maximAL View Post
    it seems when using drag 'n drop with trees, the structure of the items gets flattened to a list.
    Yes, that's unfortunate. I'm afraid you will have to reimplement QTreeWidget::mimeData() and QTreeWidget::dropMimeData() and store items into MIME data in hierarchical format to achieve that.
    J-P Nurmi

Similar Threads

  1. Installation on Fedora Core 4
    By jcr in forum Installation and Deployment
    Replies: 3
    Last Post: 29th January 2009, 01:34
  2. dummy question(Error)
    By Masih in forum Qt Programming
    Replies: 12
    Last Post: 19th July 2007, 23:38

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.