[Qt-qml] Dummy model for QList<QObject*> model

Cornelius Hald hald at icandy.de
Mon Sep 13 11:24:09 CEST 2010


Hi Michael,

On Mon, 2010-09-13 at 04:29 +0200, michael.brasser at nokia.com wrote:
> There is a discrepancy in how QAbstractItemModel models and QList<QObject*> models expose data (see http://doc.qt.nokia.com/4.7-snapshot/qdeclarativemodels.html#qlist-qobject and http://bugreports.qt.nokia.com/browse/QTBUG-13576 for more). To work around that, you'll need your dummy data to be a list of objects rather than a proper model, to mimic what you are doing in C++. Here's an example of how your could modify your above code to do that:

thanks for the pointers. I've subscribed to the ticket you mentioned.

> // MyObject.qml
> import Qt 4.7
> QtObject {
>     property string name
>     property color color
> }
> 
> // dummydata/myModel.qml
> import Qt 4.7
> QtObject {
>     property list<QtObject> dataObjects:[
>         MyObject {
>             name: "Dummy Peter"
>             color: "orange"
>         },
>         MyObject {
>             name: "Dummy Paul"
>             color: "yellow"
>         }
>     ]
> }

If I do it like above, I get errors in myModelqml.
'name' is not a valid property name
'color' is not a valid property name

I tried it both with QtObject and with Item with the same result. So use
the ListModel and try to change the C++ side.

> Alternatively, you could use a model rather than a list of objects on the C++ side, and then model.color, model.name, etc should work for both. If you'd like to go this route you might be interested in QObjectListModel (http://qt.gitorious.org/qt-labs/qml-object-model), which is meant to be a more powerful alternative to QList<QObject*> that retains much of the ease of use of working with a simple list.

I tried it with QObjectListModel, but I get the following error:
QMetaProperty::read: Unable to handle unregistered datatype
'QObjectListModel' for property 'MyModel::dataObjects'

I also tried changing the return value of the property to
QAbstractItemModel and QObject. But with the same outcome.

I will now try to implement my own QAbstractItemModel like in this
example[1]. However, I would prefer a more generic way. In fact the
QObjectListModel really looks like the right thing. Do you have any idea
what I'm doing wrong?

Thanks!
Conny

[1]
http://gutenberg.troll.no/4.7-snapshot/declarative-modelviews-abstractitemmodel.html




More information about the Qt-qml mailing list