[Development] QML and QAbstractListModel

André Somers andre at familiesomers.nl
Mon Jan 14 09:29:41 CET 2013


Op 10-1-2013 17:46, Alberto Mardegan schreef:
> Hi all!
>     I'd like to make C++ models more usable from QML; in the net there
> are several blog posts illustrating how to achieve that, but IMHO it
> would be better if at least some of these handy features were in
> QAbstractListModel itself:
>
> - "count" property
> - "get(index)" invocable method, returning a QVariantMap mapping all the
> roles to their data for the specified index
>
> Also, though not very often requested, we could add:
> - "remove(index)" which just calls QAbstractItemModel::removeRow()
> - "QList<QVariant> items(const QString &role)", which returns the data
> for the role "role" for all items.
>
> The implementation for the above is fairly trivial, so I wonder if
> there's some other reasons why it hasn't been done or if it's just that
> no one did and a patch is welcome.

I am not fan of this change. I think the API of QAIM is already very 
complex. Adding more methods that basically only sort-of mirror existing 
methods but for a more confined use-cases only makes that situation 
worse. For instance, you now get a count() method that claims that it 
returns the number of items in the model. What does that mean in the 
context of a tree model? It makes QAIM even harder to understand for 
people trying to get into writing models for Qt's item views.

If you really need a these methods exported to QML, I'd like to see that 
done in a way that does not further complicate the C++ API.

Perhaps an alternative approach would be to create a proxy model for 
exporting to QML. It could include all the methods you need, but it 
would not complicate QAIM itself. It seems to me that all the methods 
you need can be implemented in terms of the already existing 
functionality in QAIM, so it would work on all of them. Instead of 
exposing the C++ QAIM directly, you could create an instance of the 
proxy on it and expose that proxy model to QML instead, providing all 
the QML specific methods you need without complicating the base QAIM.

It would IMHO be even better to get access to the QML model API from 
C++, making it possible to build 'native' QML models directly in C++. 
Eventually, that will probably happen, but I guess more water will need 
to flow through <insert your local major river> before that can be done.

André




More information about the Development mailing list