[Qt-qml] QML/C++ shared QItemSelectionModel

Stephen Kelly steveire at gmail.com
Tue Sep 14 16:16:08 CEST 2010


Carl Snellman wrote:

> Hey,
> 
> I have one common QAbstractItemModel that serves data for a ListView
> in QML side and for another QGraphicsWidget (a map that shows items).
> They use the same data model nicely, but as my goal is to keep the
> selection in sync in both views, so I'm trying to get them to share
> the selection model, too. My problem is that I cannot find a way to
> get those two views to share the same selection model
> (QItemSelectionModel, acquired with map.selectionModel()). Would
> anyone have any suggestions/snippets how to hook my own
> QItemSelectionModel to QML's ListView?
> 
> 
> Any help is greatly appreciated!
> Carl


I agree. QML should definitely make some use of QItemSelectionModel or at 
least decouple selection from the view (I'm not sure if it does that at 
all).

My solution is a wrapper around a QItemSelectionModel with some slots:

public slots:
  void clearSelection();
  void select(int row, int command);

I had to make command an int because there was so way to use 
QItemSelectionModel::SelectionFlags at the time. I don't know if that has 
changed.

I also needed some Connections{} elements in the QML. There should 
definitely be something built in though, as I spent some time trying to 
convince some trolls of :).

All the best,

Steve.



More information about the Qt-qml mailing list