ListView (QML) and QItemSelectionModel (C++)
-
wrote on 3 Jan 2019, 18:36 last edited by fcarney 1 Mar 2019, 18:40
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?
http://doc.qt.io/qt-5/qml-qtquick-listview-members.html
I want to use it the way it is used in this code:
http://doc.qt.io/qt-5/qtwidgets-itemviews-chart-mainwindow-cpp.htmlThe code calls setSelectionModel on the views that use it. I don't see an equivalent property for ListView in QML.
-
wrote on 3 Jan 2019, 18:51 last edited by
I see that there is a QML type:
https://doc.qt.io/qt-5.11/qml-qtqml-models-itemselectionmodel.htmlIt says it can be used with the model and any views. It also points me to:
https://doc.qt.io/qt-5.11/qtquick-modelviewsdata-modelview.htmlHowever I am not seeing how to use it with properly either.
-
wrote on 3 Jan 2019, 20:29 last edited by
Well it looks like on the QML side you have to do some work in a mouse click event:
https://stackoverflow.com/questions/41566115/purpose-and-usage-of-itemselectionmodel-in-qmlIn 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.
-
wrote on 3 Jan 2019, 22:23 last edited by
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.
-
wrote on 4 Jan 2019, 10:23 last edited by
Have you look at this ? :
http://doc.qt.io/qt-5/model-view-programming.html#handling-selections-in-item-views -
wrote on 4 Jan 2019, 14:44 last edited by
@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:
table->setSelectionModel(selectionModel);
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.
1/6