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 ?

6
  • Why do you need it this way? Do you really need whole ListModel or maybe only the value of one element? Commented Nov 23, 2017 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. Commented Nov 23, 2017 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. Commented Nov 23, 2017 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. Commented Nov 23, 2017 at 15:27
  • 1
    class Q_QML_PRIVATE_EXPORT QQmlListModel : public QAbstractListModel => This means you can use the QAbstractListModel-API to read out everything you need. Also to alter the content. Commented Nov 23, 2017 at 15:39

1 Answer 1

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!"; }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, however, how can i call these methods on the created *QObject ? I tried casting (QAbstractListModel)object, qobject_cast<QAbstractListModel>(object), or reinterpret_cast<QAbstractListModel>(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 ?
Yep, you need to use qobject_cast<QAbstractListModel*>(object) instead of qobject_cast<QAbstractListModel>(object). It will be a pointer.
For some reason, I don't know, itemData seems to fail. Retrivel via data seems to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.