How to make a TableView or TreeView with a cell delegate chosen according to the value of another cell?
The idea is to make a property editor similar to this:
I tried various of the approaches listed here: https://doc.qt.io/qt-5/qml-qt-labs-qmlmodels-tablemodel.html
However DelegateChooser
can only choose based on column or based on roleValue. None of those would work for the above usecase.
The model could be something like this:
model: TableModel {
TableModelColumn { display: "name" }
TableModelColumn { display: "value" }
rows: [
{
name: "Name",
type: "string",
value: "Alfred"
},
{
name: "Amount",
type: "float",
value: 3.75
},
{
name: "Enabled",
type: "bool",
value: true
},
{
name: "Count",
type: "int",
value: 2
},
{
name: "Color",
type: "color",
value: "#3300ff"
}
]
}
to show a 2-column table view, where the delegate in the second column is chosen according to the value of type.
Even selecting on the name role (which is a suboptimal solution, because there will be many properties of each type, and each DelegateChoice
should match multiple names) does not work:
delegate: DelegateChooser {
role: "name"
DelegateChoice {
roleValue: "Enabled"
delegate: CheckBox {
checked: model.display
onToggled: model.display = checked
}
}
DelegateChoice {
roleValue: "Count"
delegate: SpinBox {
value: model.display
onValueModified: model.display = value
}
}
DelegateChoice {
delegate: TextField {
text: model.display
selectByMouse: true
implicitWidth: 140
onAccepted: model.display = text
}
}
}