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
    

    Help??



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



  • According to this (http://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html) "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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.