[Development] Making ItemSelectionModel more useful

deDietrich Gabriel Gabriel.deDietrich at theqtcompany.com
Mon Feb 22 23:31:23 CET 2016


On Feb 20, 2016, at 4:19 AM, Alberto Mardegan <mardy at users.sourceforge.net> wrote:
> 
> Hi all!
>  Since Qt 5.5, the QItemSelectionModel class is exposed to QML as
> ItemSelectionModel (in the QtQml.Models module):
> 
> http://doc.qt.io/qt-5/qml-qtqml-models-itemselectionmodel.html
> 
> Apart from having almost no documentation, this component seems to be
> rather hard to use in QML: its methods work with QModelIndex (while all
> the models I've ever used in QML just speak row numbers) or
> QItemSelection, which I believe is not usable in QML at all (at least,
> it's not documented as a QML component).
> 
> I understand that the reason for all of this is that QItemSelectionModel
> is a class originally created for C++ consumption, and that the
> QModelIndex makes a lot of sense there. However, I would argue that the
> QML version is hardly usable.
> 
> As I see it, either this class gets a "model" property and methods which
> operate on integers (row numbers), or QModelIndex and QItemSelection
> need to become first class citizens in QML as well. I guess that having
> Q_GADGET is already a huge step forward, but we should also have a
> generic way to turn a model+rowNumber into a QModelIndex: maybe a global
> method Qt.makeIndex(model, row), which would also work on all models
> supported by the QML engine?

QModelIndex is a rather simple class that’s closely tied to QAbstractItemModel. On the Qt Quick side, when you assign a model to a ListView, for example, Qt Quick will wrap it into an internal class. That class can work with both QAIM or a JS array (and others) and deals with the view in an opaque (to the view) way. In the latter case, there's no QAIM at all, so there's no way we can make a QModelIndex out of it. Whether that would be a wise thing to do internally is debatable.

The story behind having QModelIndex and QItemSelectionModel exposed to the QML engine was to solve the selection problem in the Qt Quick Controls 1 TreeView. TableView could use a simple selection model because of the trivial mapping between table row and model index. That’s not possible for the TreeView, so we decided to reuse what was already available in the QtWidgets side. Note that QAbstractItemModel::index() and several more functions have been made invokable. So those are available in QML provided your model is QAIM-based.

Best regards,

Dr. Gabriel de Dietrich
Senior Software Developer
The Qt Company — www.qt.io

> Are there any plans about this, or what would be the best way to address
> this?
> 
> Ciao,
>  Alberto
> 
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development



More information about the Development mailing list