Search code examples
delegatesqmltableviewqtquickcontrols2

QML Distinctive TableViewColumn Delegate


I have a TableView object which consists of four TableViewColumn objects.

Two of the TableViewColumn objects' delegates are ComboBox.

I want the second delegate to change its model depending on the user's choice in the first delegate. I have managed this.

However, this results in changing the second delegate's model in all of the other rows of the table. I want this change to be applied only in the respective row so as to be able to have different models in each row.

Is this possible to happen? And if so, how?

TableViewColumn{
            id: usageCol
            title: "Type"
            property string modelName: "Power Out"
            delegate: ComboBox {
                id: usageCombo
                model:
                    ListModel{
                    id: usageModel
                    ListElement { text: "PowerOut" }
                    ListElement { text: "AnalogOut" }
                    ListElement { text: "PwmOut" }
                    ListElement { text: "DigitalOut" }
                    ListElement { text: "BldcOut" }
                    ListElement { text: "AnalogIn" }
                    ListElement { text: "PwmIn" }
                    ListElement { text: "DigitalIn" }
                    ListElement { text: "Can" }
                }
                currentIndex: 0
                height: 16
                anchors.fill: parent
                onCurrentTextChanged: {
                    tableModel.setProperty(styleData.row,"use",currentText);
                    usageCol.modelName = currentText;
                }
            }
            width: tableConfig.width/tableConfig.columnCount
        }
TableViewColumn{
            id: pinCol
            title: "PIN"
            property string modelName: usageCol.modelName
            delegate: ComboBox {
                id: pinCombo
                model: {if (pinCol.modelName === "PowerOut") { modelPowerOut;}
                    else if (pinCol.modelName === "AnalogOut") { modelAnalogOut;}
                    else if (pinCol.modelName === "AnalogIn") { modelAnalogIn;}
                    else if (pinCol.modelName === "PwmOut") { modelPwmOut; }
                    else if (pinCol.modelName === "DigitalOut" || pinCol.modelName === "BldcOut" || pinCol.modelName === "DigitalIn") { modelDigitalBldc; }
                    else if (pinCol.modelName === "PwmIn") { modelPwmIn; }
                    else if (pinCol.modelName === "Can") { modelCan; }
                }
                /* Try Loader for delegate changing the model as needed. */
                height: 16
                anchors.fill: parent
                onCurrentTextChanged: {
                    tableModel.setProperty(styleData.row,"pin",currentText);
                }
            }
            width: tableConfig.width/tableConfig.columnCount
        }

[SOLVED]

TableViewColumn{
            id: pinCol
            title: "PIN"
            delegate: ComboBox {
                id: pinCombo
                model:
                {
                    switch(tableModel.get(styleData.row).use){
                    case "PowerOut": return modelPowerOut
                    case "AnalogOut": return modelAnalogOut
                    case "AnalogIn": return modelAnalogIn
                    case "PwmOut": return modelPwmOut
                    case "DigitalOut" || "BldcOut" || "DigitalIn": return modelDigitalBldc
                    case "PwmIn": return modelPwmIn
                    case "Can": return modelCan
                    }
                }

                height: 16
                anchors.fill: parent
                onCurrentTextChanged: {
                    tableModel.setProperty(styleData.row,"pin",currentText);
                }
            }
            width: tableConfig.width/tableConfig.columnCount
        }

Solution

  • You can not use modelName as it is a common property for the whole column. You should rely on the model from the main TableView instead (corresponding to a row).

    Removing all modelName properties and using tableModel.get(styleData.row).use instead of pinCol.modelName should fix your problem.