Context menu for tree view: Accessing model index in action method
-
Hi,
I created a custom context menu for a tree view (customContextMenuRequested). This works fine. Inside the customContextMenuRequested method I have access to the model index. The actions in the context menu have their own slots. What is the appropriate way to pass the model index to this slots?
Remoneo
-
Hi and welcome to devnet,
One way to do it is when you create the menu you can set the model index as data on your QAction and in your slot you can retrieve that index.
Hope it helps
-
I'd advise against doing that. You should not store a QModelIndex. In the time that your context menu is visible and waiting for user interaction, the underlying model may already have changed and index be invalidated. Also, this requires your slot to use the sender() function to get a pointer to the QAction that triggered the action. I'd try to avoid that. If you use C++11, you could use a lambda instead though for your connection. That would avoid having to store anything in the action itself.
There are two options: if you do want to store the QModelIndex, use a QPersistentModelIndex. Otherwise, simply query QItemSelectionModel again at the moment of execution to get the current item or the selected item(s) to operate on. These will be updated properly.
-
Andre is completely right and I just saw that what I wrote wasn't what I had in mind.
I was thinking about the index coordinates as in (row, column) but that word didn't reach the keyboard.
-
[quote author="SGaist" date="1422951027"]Andre is completely right and I just saw that what I wrote wasn't what I had in mind.
I was thinking about the index coordinates as in (row, column) but that word didn't reach the keyboard.[/quote]
Even those might not be what you want. What if you click on (3,0) and you get your context menu, and then the model decides to insert a new row at the top of the model? That will move the row you just clicked on down to (4,0). Now you click the delete option in that menu, and... where did my (what used to be) row 2 go? Or the other way around: what if the model decided to remove row 0 in the meantime, and now there is no row 3 any more (the item you clicked became row 2 when the model removed row 0).
-
[quote author="Andre" date="1422951693"][quote author="SGaist" date="1422951027"]Andre is completely right and I just saw that what I wrote wasn't what I had in mind.
I was thinking about the index coordinates as in (row, column) but that word didn't reach the keyboard.[/quote]
Even those might not be what you want. What if you click on (3,0) and you get your context menu, and then the model decides to insert a new row at the top of the model? That will move the row you just clicked on down to (4,0). Now you click the delete option in that menu, and... where did my (what used to be) row 2 go? Or the other way around: what if the model decided to remove row 0 in the meantime, and now there is no row 3 any more (the item you clicked became row 2 when the model removed row 0). [/quote]
Indeed, it was just a correction of my original proposition and nothing more. Using the selection model is safer. One thing I see with the model being updated between the context menu call and the slot invocation is that the selection might get lost depending on how the update was done.
-
That will only happen if the model is reset, or the item you had selected was removed from the model.
Resetting models is not something you should do if you can avoid it at all, and if the model removed the item you had selected, then you cannot operate on it anymore anyway right?
-
Yep, 100% right !