Updating model from QML delegate
-
What is the now recommended way to both access and update a C++ model's data inside a QML delegate?
In the interest of better organising my code, I've moved my ListView's delegate into its own file, which means I can no longer use model.myRole inside the delegate.
I've tried using required properties to pass each model role into the delegate but this appears to be read-only and does not allow me to update the model from the delegate.
The most obvious solution to be would be to simply expose the entire model to the delegate via a property. Would this be appropriate in this case, or should I explore other options?
-
There are two ways of doing that with required properties:
- define a
required property QtObject modeland then you can write to your roles withmodel.role = value - Since Qt 6.10, views have a new property
delegateModelAccessand when set to ReadWrite you can write directly to required role properties.
More info here : https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html#changing-model-data
- define a
-
There are two ways of doing that with required properties:
- define a
required property QtObject modeland then you can write to your roles withmodel.role = value - Since Qt 6.10, views have a new property
delegateModelAccessand when set to ReadWrite you can write directly to required role properties.
More info here : https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html#changing-model-data
- define a
-
The first solution works pre 6.10.
It puzzles me why the readwrite mode wasn't the default mode from the start though.
-
E ECEC has marked this topic as solved