Please nominate your Qt Champions for 2021!

ListModel defined in C++, trying to print count in QML

  • I have a ListModel in C++ defined as

    QList<QObject*> myModel
    setContextProperty("myModel", QVariant::fromValue(myModel));

    On the QML side i want to see how many items are in the list... but when i try something like

    console.log("list size: " + myModel.count)

    all i see print out is

    list size: undefined


  • @poncho524 Did you try myModel.count()? It's a function, not a property.

  • According to this ( "count" is a property.

    However, I did try count() and it gave a bigger error.

  • @poncho524 But the documentation you gave is for the QML ListModel. It's different than a C++ context property model. Maybe you should inspect the QML tree at runtime with the QML debugger and check out what your model object really is. You may be able to handle it as a javascript object.

    Actually QList doesn't have properties or slots and it's not even a QObject so you can't use its functions in QML. It's possible that you have to implement a QAbstractItemModel C++ model if you want to handle the model in both C++ and QML.

Log in to reply