Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
ListView (QML) and QItemSelectionModel (C++)
fcarney last edited by fcarney
I have a ListView in QML that is accessing a QAbstractListModel from C++. I want to use the QItemSelectionModel with the ListView, but I cannot find the property for the selection model. Does the QML ListView not support that?
I want to use it the way it is used in this code:
The code calls setSelectionModel on the views that use it. I don't see an equivalent property for ListView in QML.
I see that there is a QML type:
It says it can be used with the model and any views. It also points me to:
However I am not seeing how to use it with properly either.
Well it looks like on the QML side you have to do some work in a mouse click event:
In the delegate for each listview item you need a MouseArea that looks for clicks. Then this will get an index (int) that needs to be converted to a modelindex by calling index on the listmodel. This is then given to the Q/ItemSelectionModel. I am using a C++ one, but the usage is the same on the QML side.
For some reason I had it in my head that the selection could be inferred through accessing the listmodel only. I guess the listmodel just doesn't have the information to do that.
Is QItemSelectionModel supposed to allow views to be synced? I thought this would be a way to make sure all lists that select an item from a list would be synced together. Since there is no direct connection between a ListView and an ItemSelectionModel I don't know how this could work.
Nitroboy last edited by
Have you look at this ? :
@Nitroboy, thank you. I had not read that specific section. That still only covers the widgets side of things in that documentation.
In the widgets side I can do this:
Where table is a view. This makes table aware of the QItemSelectionModel. I don't see an equivalent for ListView in QML.
I am thinking I am overthinking this. I can only select one item at a time. I can store this selection whenever the view is clicked. Just storing an index should be sufficient. I had hoped that I could use QItemSelectionModel as a convenient place to store this as it would automagically track this change. I don't see that happening with the ListView in QML.