0

I have a ListModel:

ListModel {
  ListElement {
    property: "value"
  }
  ListElement {
    property: "value2"
  }
}

which I am trying to access from a c++ Qt class.

I've managed to get a reference to the listmodel:

QQmlEngine engine;
QQmlComponent component(&engine,
            QUrl("qrc:///path.qml"));
QObject *object = component.create();

Debbuging the object gives me a QQmlListModel(adress).
object -> chlidren() gives me nothing, object -> children().count() shows 0.
I tried making a QList or QTableView from the object, but with no luck.

How can I get the values of the ListElements ?

Piotr Adam Milewski
  • 14,150
  • 3
  • 21
  • 42
  • Why do you need it this way? Do you really need whole ListModel or maybe only the value of one element? – Xplatforms Nov 23 '17 at 14:43
  • @Xplatforms I've got a ListModel with different ListElements - same properies, but different values. I need a cpp counterpart - for now i populate a QAbstractListModel using invokable methods, but i was wondering if this could be done in the constructor of the class. – Piotr Adam Milewski Nov 23 '17 at 14:48
  • Why do you want to send data from QML to c ++? that is not recommended, if you give us a broader context we could suggest other better solutions. – eyllanesc Nov 23 '17 at 15:20
  • Facing such issue means you have an application design problem. C++ implementation of `ListModel` is private and should not be used. Using custom QAbstractItemModel implementation could solve the problem. – folibis Nov 23 '17 at 15:27
  • I know this is backwards to what it should be. I'm not calling the shots, and I'm not authorized to alter this by much. If this becomes a major issue, rewriting the QML ListModel to a C++ QAbstractionItemModel will be the next course of action, until then, i just wanted to know if i can suck out data from the ListModel, to keep things in one place. – Piotr Adam Milewski Nov 23 '17 at 15:33
  • 1
    [`class Q_QML_PRIVATE_EXPORT QQmlListModel : public QAbstractListModel`](https://code.woboq.org/qt5/qtdeclarative/src/qml/types/qqmllistmodel_p.h.html) => This means you can use the `QAbstractListModel`-API to read out everything you need. Also to alter the content. – derM - not here for BOT dreams Nov 23 '17 at 15:39

1 Answers1

4

As QQmlListModel inherits QAbstractItemModel you can use all methods provided and implemented by this class.

More specifically you will be looking for:

Then you can easily iterate over the model.

QQmlComponent component(&engine, "MyQmlListModel.qml");
QObject* o = component.create();
QAbstractListModel* m = qobject_cast<QAbstractListModel*>(o);

if (m != nullptr) {
    qDebug() << m->rowCount();
    qDebug() << m->data(m->index(0, 0), 0);
}
else { qDebug() << "failed!"; }
  • Thanks, however, how can i call these methods on the created *QObject ? I tried casting (QAbstractListModel)object, qobject_cast(object), or reinterpret_cast(object), but all i get is invalid cast errors, or abstract return types. Should I make a new class inheriting from QAbstractListModel, and cast the object on the new class ? – Piotr Adam Milewski Nov 24 '17 at 08:41
  • Yep, you need to use `qobject_cast(object)` instead of `qobject_cast(object)`. It will be a pointer. – derM - not here for BOT dreams Nov 24 '17 at 12:15
  • For some reason, I don't know, `itemData` seems to fail. Retrivel via `data` seems to work. – derM - not here for BOT dreams Nov 24 '17 at 12:25