View->Model and selection...
-
It seems there is zero built-in selection support with my choice of QListView -> QAbstractListModel.
Do I have to write everything from scratch? the catching of a selection event in the UI, the marking of the model item as selected, etc? It seems there is no out-of-the-box support for this.
the weird thing is that there is a QItemSelectionModel that does support this, but you cannot use it with QListView as it's not derived from QAbstract....
Should my model class use multiple inheritance to inherit both from QItemSelectionModel and QAbstractListModel? Otherwise I don't see how I can avoid having to re-writing this functionality myself.
My final goal is for the delegate that draws my items to know if the item is selected, both in the paint and the sizeHint function.
-
"QAbstractItemView::selectionMode":http://doc.qt.nokia.com/4.7/qabstractitemview.html#selectionMode-prop does the stuff
The selection is part of the view not of the model!
-
Well... if you look carefully at your item views, you see that they have both a data model (a QAbstractItemModel) and a QItemSelectionModel. You don't seriously think that Qt would not support these basic features, if they are shown off in the samples for the item views?
-
so, my QListView's ancestor has: "virtual void setSelectionModel ( QItemSelectionModel * selectionModel )"
But what does that mean? should I just set is with a "new QItemSelectionModel"?
terminology here is strange. A Model is DATA. Why do I have to set two models for my view if I just want to access selection information? what's the logic behind this? have another model where the VIEW will write its selection information? Wouldn't it be simpler to allow the user to derive an item model that already has the selection defined in its metadata?
-
hi ronM71,
just read the docs:
QListView derives from QAbstractItemView, and QAbstractItemView owns a selectionModel.
The delegate gets everything via QStyleOptionViewItem and QModelIndex.QStyleOptionViewItem is derived from QStyleOption and "QStyleOption::state":http://doc.qt.nokia.com/4.7/qstyleoption.html#state-var and "state":http://doc.qt.nokia.com/4.7/qstyle.html#StateFlag-enum contains QStyle::State_Selected
-
Every view has a selection model by default. However, being able to set one, makes it possible to share such a model between different views. Thus keeping selections synchronized between the two (or more) views. It is not as bizare as you think :-)
And yes: a model does contain data. Data on selections.
-
If you look through "this":http://doc.qt.nokia.com/4.7/model-view-programming.html document, you get all this information about delegates, selections etc.