PDA

View Full Version : QTreeView Drag and Drop



theprobe
29th March 2011, 12:30
Hi,
I have two tree views, side by side and I want to drag one item from one tree into the other. So far I have managed to get something working but my problem is that for the QModelIndex that I drag from Tree A, I want its internal pointer to be available in Tree B.

So far the only way I can see to do it is put the pointer into a QByteArray in


QMimeData *MyModel::mimeData(const QModelIndexList &indexes) const

and read it back out in


bool MyModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
int row, int column, const QModelIndex &parent)

Is this really the best way? Streaming pointers feels wrong.

Thanks

ChiliPalmer
29th March 2011, 22:08
You could also have the two TreeViews communicate.

When you start the drag you could save the modelIndex that would be dragged from the Tree. In the other Tree, when there is a drop, you could use QDropEvent::source() to test if the drag comes from the other treeWidget and then just take the saved modelIndex from it.