Solved How do I get the items selected from a QTreeview?
-
I am trying to drag an item from the treeview to listview.
listview is promoted and thedropEvent
is shown below.
How can I get the dropped item from the row , col or mimedata ?void dropEvent(QDropEvent* event) { const QMimeData* mime = event->mimeData(); QByteArray encoded = mime->data("application/x-qabstractitemmodeldatalist"); QDataStream stream(&encoded, QIODevice::ReadOnly); while (!stream.atEnd()) { int row, col; QMap<int, QVariant> roleDataMap; stream >> row >> col >> roleDataMap; event->setDropAction(Qt::MoveAction); event->accept(); } }
-
Hi,
You should take a look at the Using drag and drop with item views chapter of Qt's documentation.
It explains what you need to handle that.
-
@SGaist
Thanks for the reply. Qt's documentation was helpful.
I was able to find the index of selected items in thedropEvent
usingQTreeView * view= dynamic_cast<QTreeView*>(event->source()); QModelIndexList list= view->selectionModel()->selectedIndexes();
Is there any wayto do it in mimeData?
-
That's really not the role of mime data. What do you want to do with that information ?
-
@SGaist
I just wanted to get the index of selected items from the tree. Just taught it can be done using mimeData or not . -
Again passing object through mime data is not the right thing to do. But without knowing what you want to do with the indexes, it's pretty difficult to find a suitable solution.
-
@SGaist
With index i want to restrict particular items to be dragged from treeview to listview -
What are the restriction rules ?
-
-
In that case, re-implement the flags method of your model. Then you can disable there the possibility to drag items that are not messages.