How to call `QAbstractItemModel::setData` from QML view?
-
I have a question regarding the ability to call
QAbstractItemModel::setDatafrom QML code. The functionsetDatahasQ_INVOKABLE, so it should be callable from QML. However, it expects aQModelIndex&as the first argument, which doesn't seem obtainable from a QML model.My question, however, is more general than this. Where can I find resources about editable Qt models? I have read this article on Editable Tree Model Example but it doesn't provide insight on the QML side.
-
If you are using required properties in your delegates, you can specify
delegateModelAccessof your view toDelegateModel.ReadWritethen writing to the required properties will automatically callsetDataon their corresponding role.
By default you can already do that via themodelgroup object.If you really want to call
QAIM::setDatamanually, you indeed have to pass aQModelIndexand a role but the index is accessible because theQAIM::indexfunction is alsoQ_INVOKABLE. -
@kaixoo said in How to call `QAbstractItemModel::setData` from QML view?:
Sorry, this isn't clear enough to me. Do you know if there is any example code about this?
See the snippet under https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html#changing-model-data
-
K kaixoo has marked this topic as solved
-
For multiple reasons, calling
setDatain this case is the only viable option. This is what I have in my head:ListView { model: /* ... */ delegate: SomeWidget { onEdit: { model.setData(model.index(row, column), someDataToMutate) } } }Column is always
0in this case, but how is one supposed to get the currentrowin a QML model-view?