Access role enum value from QML
-
Hi. I would like to know if it is possible to access current role enum value from a delegate. I would like to use this value further to check if value was changed or not by direct call to model. For sure I can just pass role name directly to checking method, but prefer some safer way to use role.
-
Hi. I would like to know if it is possible to access current role enum value from a delegate. I would like to use this value further to check if value was changed or not by direct call to model. For sure I can just pass role name directly to checking method, but prefer some safer way to use role.
@egor.utsov said in Access role enum value from QML:
I would like to know if it is possible to access current role enum value from a delegate.
Yes, it is. Reimplement
QAbstractItemModel::roleNames()in C++ and addrequired property <roleDataType> <roleName>to your delegate in QML: https://doc.qt.io/qt-6/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass -
I don't understand the question. What is
current role enum?Is this related to the model-view framework? If so, the model reports changes via various signals such as QAbstractItemModel::dataChanged. Just reference the data in a binding. There's no need to have a separate function call.
-
Hi. I would like to know if it is possible to access current role enum value from a delegate. I would like to use this value further to check if value was changed or not by direct call to model. For sure I can just pass role name directly to checking method, but prefer some safer way to use role.
@egor.utsov said in Access role enum value from QML:
check if value was changed or not by direct call to model.
Actually... what do you mean by "changed... by direct call to model"?
-
@jeremy_k @JKSH I've implement method
isDirtyin the model to check if the value in the cell was changed, but not submitted. And now I want to pass cell index into this method. But index itself is not enough, because in qml role used instead of column number. And I am asking - how to obtain role integer (not role name) that is according to role name used by delegate to access model data?
Or maybe there is the better way of how to do it. -
@jeremy_k @JKSH I've implement method
isDirtyin the model to check if the value in the cell was changed, but not submitted. And now I want to pass cell index into this method. But index itself is not enough, because in qml role used instead of column number. And I am asking - how to obtain role integer (not role name) that is according to role name used by delegate to access model data?
Or maybe there is the better way of how to do it.@egor.utsov said in Access role enum value from QML:
Or maybe there is the better way of how to do it.
That's what I'm thinking. Have you considered storing the uncommitted data in another role in the model? Either add a function to synchronize the two, or assign to the permanent role and let the view invoke setData().
Models used in QML also have columns. Heavy use of one-dimensional views and the ease of defining delegates might have led to fewer multicolumn examples.
-
@egor.utsov said in Access role enum value from QML:
Or maybe there is the better way of how to do it.
That's what I'm thinking. Have you considered storing the uncommitted data in another role in the model? Either add a function to synchronize the two, or assign to the permanent role and let the view invoke setData().
Models used in QML also have columns. Heavy use of one-dimensional views and the ease of defining delegates might have led to fewer multicolumn examples.
@jeremy_k the way with separate role for uncommited data seems more complex for me. Columns is not used in QML as far as I understand. We use roles instead. Like
model.someFieldto access cell value in a row. Using separate role for uncommited data simply means doubling of roles (two for each cell - commited data and uncommited) and at the same time you need to decide, which data to show at the moment. -
@jeremy_k the way with separate role for uncommited data seems more complex for me. Columns is not used in QML as far as I understand. We use roles instead. Like
model.someFieldto access cell value in a row. Using separate role for uncommited data simply means doubling of roles (two for each cell - commited data and uncommited) and at the same time you need to decide, which data to show at the moment.@egor.utsov said in Access role enum value from QML:
Using separate role for uncommited data simply means doubling of roles (two for each cell - commited data and uncommited) and at the same time you need to decide, which data to show at the moment.
Why is doubling the number of roles a problem? You could go even further and call
isDirty()fromdata()only (for another role), so that you don't need to call it from QML:delegate: Label { required property string committedData required property string uncommittedData required property bool isDirty text: isDirty ? uncommittedData : committedData }in qml role used instead of column number.
Not necessarily. You can absolutely use
QAbstractTableModelwithTableView: https://doc.qt.io/qt-6/qml-qtquick-tableview.html#c-models -
@egor.utsov said in Access role enum value from QML:
Using separate role for uncommited data simply means doubling of roles (two for each cell - commited data and uncommited) and at the same time you need to decide, which data to show at the moment.
Why is doubling the number of roles a problem? You could go even further and call
isDirty()fromdata()only (for another role), so that you don't need to call it from QML:delegate: Label { required property string committedData required property string uncommittedData required property bool isDirty text: isDirty ? uncommittedData : committedData }in qml role used instead of column number.
Not necessarily. You can absolutely use
QAbstractTableModelwithTableView: https://doc.qt.io/qt-6/qml-qtquick-tableview.html#c-models@JKSH the issue here is that I use custom view for my data and my model have tree-like structure. I use
DelegateModelto display different levels of it with custom delegates and the delegate is applied to the whole row of the table. -
Another way to do it if you have custom delegates anyway is to return a gadget struct for each role with a value property and an isDirty property.
-
Another way to do it if you have custom delegates anyway is to return a gadget struct for each role with a value property and an isDirty property.
@GrecKo that's an interesting approach. Thank you for the idea! This thread could be considered as solved.
-
E egor.utsov has marked this topic as solved on