What I’ve found is that currently the easiest way to know whenever the
GridView’s currently selected index has changed is to create a helper class
that has a Qt metasystem registered property:
Q_PROPERTY( int selectedIndex READ selectedIndex WRITE setSelectedIndex
NOTIFY selectedIndexChanged )
Then make your helper class accessible by QML:
qmlRegisterType< ItemSelectionModelQmlHelper >( "Models.Tools", 1, 0,
"ItemSelectionModelQmlHelper" );
Then create an instantiate of this class and expose it to QML:
QDeclarativeView* const view = new QDeclarativeView;
ItemSelectionModelQmlHelper* const helper = new ItemSelectionModelQmlHelper(
this );
view->rootContex()->setContextProperty( "_itemSelectionModelQmlHelper",
helper );
Then one can create the QItemSelectionModel and listen to the helper’s
signal, so that to set the current index of the QItemSelectionModel.
Of course as mentioned above, if you use a sorting or filtration of the data
being displayed in the GridView the currently selected GridView item’s index
may not correspond to a row in your QAbstractItemModel derived class.
This won’t be the case if you have done the filtration and/or sorting in C++
using the QSortFilterProxyModel. The problem with the non-coresponding index
will arise only if you do the sorting/filtration in QML.
It would be best if GridView has the option to specify a selection model.
↧