[Qt-qml] Dummy model for QList<QObject*> model
michael.brasser at nokia.com
michael.brasser at nokia.com
Mon Sep 13 04:29:43 CEST 2010
On 11/09/2010, at 12:10 AM, ext Cornelius Hald wrote:
> Hi,
>
> I'm exposing a QList<QObject*> property from C++ to QML. There I can
> access it, for example, with the following code.
>
> ListView {
> anchors.fill: parent
> model: myModel.dataObjects //dataObjects is QList<QQobject*>
> delegate: Rectangle {
> height: 25
> width: 100
> color: model.modelData.color
> Text { text: model.modelData.name}
> }
> }
>
> Now I've tried to put some dummy data in place to simulate the model if
> the QML code is run without C++ backend. So I've created the following.
>
> // dummydata/myModel.qml
> import Qt 4.7
> Item {
> property alias dataObjects: listModel
> ListModel {
> id: listModel
> ListElement {
> name: "Dummy Peter"
> color: "orange"
> }
> ListElement {
> name: "Dummy Paul"
> color: "yellow"
> }
> }
> }
>
> Unfortunately this is not working as expected. It only works if I remove
> the "modelData" part from the delegate. E.g. the following works.
>
> ListView {
> anchors.fill: parent
> model: myModel.dataObjects //dataObjects is QList<QQobject*>
> delegate: Rectangle {
> height: 25
> width: 100
> color: model.color // removed modelData
> Text { text: model.name} // removed modelData
> }
> }
>
> How can I create a dummy model that I can use as a drop-in replacement for my QList<QObject*> model?
Hi,
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:
// 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"
}
]
}
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.
Regards,
Michael
More information about the Qt-qml
mailing list