how to use QML ListModel in C++?
-
Hi all -
I have a QML ListModel:
ListModel { id: sceneFeatureListModel } Component.onCompleted: { for (var i = 0; i < sceneFeatureList.length; i++) { sceneFeatureListModel.append({ "uuid": sceneFeatureList[i] }) // this works. } } ... sceneModel.sendPatch(sceneCopy, sceneFeatureListModel) // this doesn't.
My sendPatch signature is:
void SceneModel::sendPatch(Scene *scene, QVariant qv) {
I can't figure out how what I should convert the QVariant to. I've tried lists and QObject, but neither work. Can I do this from this routine, or do I need to convert the ListModel contents before passing to C++?
Thanks...
-
I would recommend generally not using
ListModel
. The legit use cases are prototyping and a static model with actual roles. Populating a view only model from events is also fine.For other use cases, the better solution is a normal QAbstractItemModel, a QList or a JS array depending on the situation.
To answer the question: you can pass it as a
QAbstractListModel*
. -
I would recommend generally not using
ListModel
. The legit use cases are prototyping and a static model with actual roles. Populating a view only model from events is also fine.For other use cases, the better solution is a normal QAbstractItemModel, a QList or a JS array depending on the situation.
To answer the question: you can pass it as a
QAbstractListModel*
.@GrecKo said in how to use QML ListModel in C++?:
the better solution is a normal QAbstractItemModel, a QList or a JS array
OK, I used the JS array from which I created the ListModel - just updated it from the ListModel before sending it sendPatch().
I would use a QAIM, but I can't figure out how to write the logic. The routine that initializes my JS array (and would be used to initialize a model) looks like this:
QList<QString> SceneModel::featureUuidList(bool runningOnly, QUuid spaceUuid) { QList<QString> sceneFeatureList; QList<QString> featureUuids = m_activityModel->featureUuidList(runningOnly, spaceUuid); for (const auto &uuidString : featureUuids) { for (const auto &scene : m_list) { for (const auto &feature : scene->featureList()) { auto featureUuid = feature->featureId(); if (featureUuid.toString() == uuidString) { auto featureIdString = featureUuid.toString(); if (!sceneFeatureList.contains(featureIdString)) sceneFeatureList.append(featureIdString); } } } } return sceneFeatureList; }
I couldn't find a way to incorporate the spaceUuid into the selection logic.
-
I would recommend generally not using
ListModel
. The legit use cases are prototyping and a static model with actual roles. Populating a view only model from events is also fine.For other use cases, the better solution is a normal QAbstractItemModel, a QList or a JS array depending on the situation.
To answer the question: you can pass it as a
QAbstractListModel*
.@GrecKo said in how to use QML ListModel in C++?:
I would recommend generally not using
ListModel
. The legit use cases are prototyping and a static model with actual roles.For people who mostly work with QML and javascript, I've taken to emphatically recommending ListModel. It works. Use it until testing determines that it does not work. It eliminates all of the questions about why a view fails to update when the incorrectly implemented or not at all valid model is updated.
For C++ users, with an adequate understanding of the language, start with QStandardItemModel. It works. Use it until testing determines that it does not work. The same goes for Python, with the added performance benefit of spending less time in Python code.
a QList or a JS array depending on the situation.
These are better suited to testing and static data use cases. Over and over again, users are confused by the static model generated via ListView, TableView, etc failing to behave like a dynamic model.
-
@GrecKo said in how to use QML ListModel in C++?:
I would recommend generally not using
ListModel
. The legit use cases are prototyping and a static model with actual roles.For people who mostly work with QML and javascript, I've taken to emphatically recommending ListModel. It works. Use it until testing determines that it does not work. It eliminates all of the questions about why a view fails to update when the incorrectly implemented or not at all valid model is updated.
For C++ users, with an adequate understanding of the language, start with QStandardItemModel. It works. Use it until testing determines that it does not work. The same goes for Python, with the added performance benefit of spending less time in Python code.
a QList or a JS array depending on the situation.
These are better suited to testing and static data use cases. Over and over again, users are confused by the static model generated via ListView, TableView, etc failing to behave like a dynamic model.
-
Hi @mzimmers,
try to pass the ListModel as QAbstractListModel *Q_INVOKABLE void yourMethod(QAbstractListModel *model) { if (model) { int rowCount = model->rowCount(); ... '''