[Qt-qml] Dummy model for QList<QObject*> model
michael.brasser at nokia.com
michael.brasser at nokia.com
Tue Sep 14 01:00:58 CEST 2010
Hi Cornelius,
On 13/09/2010, at 7:24 PM, ext Cornelius Hald wrote:
> 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
Is the above the exact wording of the error message you are seeing (if so, what release version are you using)? If the objects in the list are QtObjects, I get the message "Cannot assign to non-existent property "name"", but as long as they are of a custom type that we've defined (in this case MyObject), with the corresponding custom properties, it all works as described for me (note that MyObject.qml needs to go in the root directory, rather than the dummydata directory). I'm happy to send you a tar of the files I'm testing with, if this is still something you want to pursue.
> 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?
You'll need to register the QObjectListModel type to use it in a property, using qmlRegisterType<QObjectListModel>();
Regards,
Michael
More information about the Qt-qml
mailing list