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

Cornelius Hald hald at icandy.de
Fri Sep 10 16:10:03 CEST 2010


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?

Thanks!
Conny





More information about the Qt-qml mailing list