Results 1 to 4 of 4

Thread: Subclassing QTreeWidgetItem for Drag & Drop?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2008
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Subclassing QTreeWidgetItem for Drag & Drop?

    Greetings all,
    Regular lurker, first time posting. I want to be able to allow drops only on certain items in a QTreeWidget. Is it possible to subclass QTreeWidgetItem to allow it to be a "drop target" for a drag & drop operation? The code below shows the approach I tried, but it doesn't work correctly: only the first MyTreeWidgetItem I add to the tree control accepts drops! The fact that this only 'sort of' works makes me suspect I'm bumping into a side effect of some of Qt's C++ magic. Is it possible to subclass QTreeWidgetItem to accept drops? Alternatively, if there is another way to achieve the goal of fine-grained control of which items can be "dropped on", I'd be happy to learn about that.

    Thanks!
    Paul Kerchen

    Platform/environment: Qt 4.3.3, MSVC 7.1, Windows XP

    Qt Code:
    1. class MyTreeWidgetItem : public QWidget, public QTreeWidgetItem
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyTreeWidgetItem( QTreeWidget* parent);
    7.  
    8. signals:
    9. void itemDropped( MyTreeWidgetItem* item, QWidget* source );
    10.  
    11. protected:
    12. void dragEnterEvent(QDragEnterEvent* event);
    13. void dragMoveEvent(QDragMoveEvent* event);
    14. void dropEvent(QDropEvent* event);
    15. };
    16.  
    17. MyTreeWidgetItem::MyTreeWidgetItem( TreeWidget* parent )
    18. : QWidget(parent)
    19. , QTreeWidgetItem( parent )
    20. {
    21. setAcceptDrops(true);
    22. }
    23.  
    24.  
    25.  
    26. void MyTreeWidgetItem::
    27. dragEnterEvent(QDragEnterEvent* event)
    28. {
    29. event->setDropAction(Qt::CopyAction);
    30. event->acceptProposedAction();
    31. }
    32.  
    33.  
    34. void MyTreeWidgetItem::
    35. dragMoveEvent(QDragMoveEvent* event)
    36. {
    37. event->setDropAction(Qt::CopyAction);
    38. event->acceptProposedAction();
    39. }
    40.  
    41.  
    42. void MyTreeWidgetItem::
    43. dropEvent(QDropEvent* event)
    44. {
    45. event->setDropAction(Qt::CopyAction);
    46. event->acceptProposedAction();
    47. emit itemDropped(this, event->source());
    48. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by kerchen; 24th April 2008 at 20:04. Reason: Added platform info

Similar Threads

  1. Drag & Drop QTreeWidgetItem with editors
    By Mad Max in forum Qt Programming
    Replies: 3
    Last Post: 25th January 2007, 09:09

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.