I have a C++ QAbstracListModel subclass that I have set to a QML GridView.
What I want is to know the current selection of the GridView in C++, so that I could synchronize the selection from it with another C++-based view. Usually, if both views are C++-based, then the solution is to use a common QItemSelectionModel. Since I want to use this standard approach, I am currently trying to employ it to my hybrid views’ case.
What I have currently done is to create a QItemSelectionModel, make it use my QAbstractListModel subclass and expose both to QML:
DataModel* const dataModel = new DataModel( this );
QItemSelectionModel* const dataSelectionModel = new QItemSelectionModel( dataModel );
QDeclarativeView* view = new QDeclarativeView;
view->rootContext()->setContextProperty( "dataModel", dataModel );
view->rootContext()->setContextProperty( "dataSelectionModel", dataSelectionModel );
What I have done in the QML’s GridView:
GridView {
id: gridView
model: dataModel
onCurrentIndexChanged: {
dataSelectionModel.setCurrentIndex( gridView.model.modelIndex( currentIndex ), 0x0002 | 0x0010 )
}
}
The numbers: 0×0002 | 0×0010, are:
QItemSelectionModel::Select 0x0002 // All specified indexes will be selected.
QItemSelectionModel::Current 0x0010 // The current selection will be updated.
QItemSelectionModel::SelectCurrent Select | Current
I exposed these to QML as well, but for the current test case I have given them as numbers.
What I want to achieve is to set the currently selected QModelIndex from the QML’s GridView to the common QItemSelectionModel.
Currently, I receive an error for:
GridView.qml: TypeError: Result of expression ‘gridView.model.modelIndex’ [undefined] is not a function.
Any suggestions how to achieve this?
↧