Results 1 to 2 of 2

Thread: QTreeView::dropEvent

  1. #1
    Join Date
    Mar 2012
    Qt products
    Qt4 Qt/Embedded

    Default QTreeView::dropEvent

    I try to program a QTreeView, that enables user to
    1. pick an item of class A and to drop it on class B, B will take that item and insert it into his subtree.
    2. Class A items can not be dropped on other items of class A
    Qt Code:
    1. this->setDragEnabled(true);
    2. this->setAcceptDrops(true);
    3. this->setDragDropMode(InternalMove);
    4. this->setDefaultDropAction(Qt::MoveAction);
    5. this->setDropIndicatorShown(true);
    6. this->setAnimated(true);
    7. this->setAutoScroll(true);
    8. this->setWordWrap(true);
    9. this->setAlternatingRowColors(true);
    10. this->setAutoScroll(true);
    11. QTreeView::dropEvent(event);
    To copy to clipboard, switch view to plain text mode 

    The items are dropped correctly, but the drop leaves an empty item in place from which the item was dragged. What to do about this?

  2. #2
    Join Date
    Sep 2011
    Thanked 5 Times in 5 Posts
    Qt products
    MacOS X Windows

    Default Re: QTreeView::dropEvent

    That's probably a bug in your dropEvent function.
    Are you using QAbstractItemModel or QStandardItemModel or any other?
    If you use QStandardItemModel, most probably you won't need to implement any of those methods (dragEnterEvent, dragMoveEvent, dropEvent) just implement the flags method in your model, and your good to go:
    Qt Code:
    1. Qt::ItemFlags flags(const QModelIndex &index) const;
    2. Qt::ItemFlags ChatRoomsListModel::flags(const QModelIndex &index) const
    3. {
    4. if ( == E_ClassA)
    5. {
    6. return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
    7. }
    8. else if ( == E_ClassB)
    9. {
    10. return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
    11. }
    12. else
    13. {
    14. return Qt::NoItemFlags;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    If you need a complex set of rules, you must implement the methods yourself, of course.
    Anyway, next time try to post your code, it could help anyone who's trying to help you.

    I hope this helps.

Similar Threads

  1. How to reimplement QTreeWidget::dropEvent?
    By adrianhill102 in forum Qt Programming
    Replies: 1
    Last Post: 22nd October 2010, 16:41
  2. dropEvent() is never called
    By kervich in forum Qt Programming
    Replies: 1
    Last Post: 10th September 2010, 14:13
  3. about dropEvent in mac
    By yxtx1984 in forum Newbie
    Replies: 1
    Last Post: 19th March 2010, 07:17
  4. QGraphicsScene and dropEvent
    By prof.ebral in forum Newbie
    Replies: 14
    Last Post: 6th March 2010, 10:16
  5. Advice catching dropEvent
    By DirtyBrush in forum Qt Programming
    Replies: 5
    Last Post: 15th July 2009, 17:43


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.