1 Attachment(s)
Need help regarding drag and drop in QListView.
Hi,
I am working on drag and drop in QListView. For the help i am referring to Qt example puzzle in itemviews folder. Now the problem lies when the list view is in IconMode. Let me depict it:-
Attachment 11930
Now if i drag the item in the center of two items like shown in picture, its returning invalid index. Now while dropping i want to get the index of either left item or right. From Qt documentation what i got is :-
"When a drop occurs, the model index corresponding to the parent item will either be valid, indicating that the drop occurred on an item, or it will be invalid, indicating that the drop occurred somewhere in the view that corresponds to top level of the model.
int beginRow;
if (row != -1)
beginRow = row;
We initially examine the row number supplied to see if we can use it to insert items into the model, regardless of whether the parent index is valid or not.
else if (parent.isValid())
beginRow = parent.row();
If the parent model index is valid, the drop occurred on an item. In this simple list model, we find out the row number of the item and use that value to insert dropped items into the top level of the model.
else
beginRow = rowCount(QModelIndex());
When a drop occurs elsewhere in the view, and the row number is unusable, we append items to the top level of the model."
But this applies only for QListView in listmode.
If i put my mouse pointer in between two items its giving invalid index. How should i handle that. Please help me in this regard.
Thank you very much.
Re: Need help regarding drag and drop in QListView.
Quote:
Originally Posted by
sonulohani
If i put my mouse pointer in between two items its giving invalid index. How should i handle that. Please help me in this regard.
Isn't that already covered by your else case?
Cheers,
_
Re: Need help regarding drag and drop in QListView.
Quote:
Originally Posted by
anda_skoa
Isn't that already covered by your else case?
Cheers,
_
Hi anda,
Thanks for answering. I am using drop event in QListView. Any idea how to get the parent model index?
Thank you
Re: Need help regarding drag and drop in QListView.
I am afraid I don't understand.
Isn't your code from a list view example?
Cheers,
_
Re: Need help regarding drag and drop in QListView.
Quote:
Originally Posted by
anda_skoa
I am afraid I don't understand.
Isn't your code from a list view example?
Cheers,
_
No its not. I mean i have taken that scenario, however in my condition i am implementing dropEvent in QListView. I am sorry for making it confusing. However i am not using dropMimeData from QAbstractItemModel. I have modified the puzzle example to work with drag and drop event function instead of giving model view to handle it.
Re: Need help regarding drag and drop in QListView.
Your QDropEvent gives you the position of the drop. The QListView methods indexAt(), visualRect() / rectForIndex() will help with mapping that position to a QModelIndex. QListView::modelColumn() tells you which column of the model is being displayed in the view. QAbstractItemView::model() gives you the pointer to the model. QAbstractItemModel::index() with arguments 0, 0 QModelIndex() gives you the root index (parent) in the model. You can iterate over all of the QModelIndex entries in the model until you find the two (or four) that bracket the drop location.
Go from there.
Re: Need help regarding drag and drop in QListView.
Quote:
Originally Posted by
d_stranz
Your QDropEvent gives you the position of the drop. The QListView methods indexAt(), visualRect() / rectForIndex() will help with mapping that position to a QModelIndex.
QListView::modelColumn() tells you which column of the model is being displayed in the view. QAbstractItemView::model() gives you the pointer to the model.
QAbstractItemModel::index() with arguments 0, 0 QModelIndex() gives you the root index (parent) in the model. You can iterate over all of the QModelIndex entries in the model until you find the two (or four) that bracket the drop location.
Go from there.
I am very very thankful to you man. You made my day. The hint you have given solved my problem. Thanks to anda_skoa also.