PDA

View Full Version : QTreeView::dropEvent



Wiplash
2nd June 2012, 11:26
Hello
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


this->setDragEnabled(true);
this->setAcceptDrops(true);
this->setDragDropMode(InternalMove);
this->setDefaultDropAction(Qt::MoveAction);
this->setDropIndicatorShown(true);
this->setAnimated(true);
this->setAutoScroll(true);
this->setWordWrap(true);
this->setAlternatingRowColors(true);
this->setAutoScroll(true);
QTreeView::dropEvent(event);


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?

tferreira
4th June 2012, 11:49
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::ItemFlags flags(const QModelIndex &index) const;
Qt::ItemFlags ChatRoomsListModel::flags(const QModelIndex &index) const
{
if (index.data(E_TypeRole) == E_ClassA)
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
}
else if (index.data(E_TypeRole) == E_ClassB)
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
}
else
{
return Qt::NoItemFlags;
}
}


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.