ListModel defined in C++, trying to print count in QML
-
wrote on 4 May 2017, 18:39 last edited by
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??
-
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??
wrote on 4 May 2017, 20:28 last edited by@poncho524 Did you try myModel.count()? It's a function, not a property.
-
wrote on 4 May 2017, 20:36 last edited by
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.
-
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.
wrote on 4 May 2017, 23:35 last edited by@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.
1/4