[QML/Qt 6.2.4 C++ Model] Required properties bindings and contentItem model
-
wrote on 10 Dec 2023, 22:43 last edited by
Dear all,
I'd like using a C++ model and show its data in two columns:
- Left column (list): ListView where, for each row, I put a Title
- Right column (detail): a set of different QML components where, for each component, I show more model data in details
I'm using roles in the C++ model and, for each role, in the delegate item of the ListView I use required properties in order to create a binding
The target is the follow: modify items on 'right column' and 'send' updated data to model.
I follow this idea (even if I don't know if it is correct): when I change currentItem on left column, I send its model (which has C++ roles bound to required properties) to right column and, for each Component, when I change data, I send updated data to model.
This is the (possible) implementation:
// left column ListView { ... model: controller.sampleModel // <-- C++ model delegate: CustomDelegate { required property var model required property int index required property string title required property int data1 required property int data2 ... text: title ... } onCurrentItemChanged: { //<-- send model to right column (model with required properties and theirs bindings) if (rightColumn.model !== currentItem.model) rightColumn.model = currentItem.model // <--- use of currentItem } } // right column ColumnLayout { id: rightColumn property var model TextField { text: model.title; onTextChanged: model.title = text } TextField { text: model.data1 } TextField { text: model.data2 } }
My questions are:
- is this feasible or does exist any drawback?
- using
currentItem
, theoricaly it has always correctly usable (even if that item goes out of visible items), right? so also in this case, things should work
In this way, viewing and changing model data from 'Right column' becomes very easy and clean; what do you think?
Thanks in advance!
Nicola
1/1