首页 > 解决方案 > Enable 属性不适用于 tableview 和 delegateChooser QML

问题描述

import QtQuick 2.15
import QtQuick.Controls 2.15
import Qt.labs.qmlmodels 1.0

ApplicationWindow {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    TableView {
        anchors.fill: parent
        columnSpacing: 1
        rowSpacing: 1
        clip: true

        model: TableModel {
            id:tableModelId
            TableModelColumn { display: "name" }
            TableModelColumn { display: "surname" }
            TableModelColumn { display: "age" }
            TableModelColumn { display: "adresse" }
            TableModelColumn { display: "profil" }
            TableModelColumn { display: "status" }

            rows: [
                {
                    "name": "max",
                    "surname": "myller",
                    "age": 16,
                    "adresse": "California",
                    "profil": "students",
                    "status": false
                },
                {
                    "name": "max",
                    "surname": "myller",
                    "age": 16,
                    "adresse": "California",
                    "profil": "students",
                    "status": false
                },
                {
                    "name": "max",
                    "surname": "myller",
                    "age": 16,
                    "adresse": "California",
                    "profil": "students",
                    "status": false
                }
            ]
        }

        delegate: DelegateChooser {
            id: delegateID
            DelegateChoice {
                column: 0
                delegate: Rectangle {
                    implicitWidth: 100
                    implicitHeight: 50
                    color: "dodgerblue"
                    TextField {
                        id:nameId
                        text: model.display
                        font.pixelSize: 10
                        anchors.centerIn: parent
                        //                        enabled:
                        //                        Component.onCompleted: strategyChoice = strategyTextId.currentText
                        //                        onActivated:strategyChoice = strategyTextId.currentText
                    }
                }
            }
            DelegateChoice {
                column: 1
                delegate: Rectangle {
                    implicitWidth: 100
                    implicitHeight:50
                    TextField {
                        text:model.display
                        font.pixelSize: 10
                        anchors.centerIn: parent
                        // It doesn't work
                        enabled:(model.status) ? true : false

                    }
                }
            }
            DelegateChoice {
                column: 2
                delegate: Rectangle {
                    implicitWidth: 100
                    implicitHeight:50
                    ComboBox {
                        anchors.centerIn: parent
                        model: ["1","2","3","4","5","6","16"]
                    }
                }
            }
            DelegateChoice {
                column: 3
                delegate: Rectangle {
                    id: rectangle
                    implicitWidth: 100
                    implicitHeight:50
                    TextField {
                        font.pixelSize: 10
                        text: model.display

                        anchors.left: parent.left
                        anchors.right: parent.right
                        anchors.top: parent.top
                        anchors.bottom: parent.bottom

                        placeholderText: qsTr("Enter balance")

                    }
                }
            }
            DelegateChoice {
                column: 4
                delegate: Rectangle {
                    implicitWidth: 100
                    implicitHeight:50
                    ComboBox {
                        id:profilId
                        model:["Students","engineers","lawyer","doctor"]
                        anchors.left: parent.left
                        anchors.right: parent.right
                        anchors.top: parent.top
                        anchors.bottom: parent.bottom
                        //  it doesn't work
                        enabled: (controlactivate.text === "OFF") ? true : false;
                    }
                }
            }
            DelegateChoice {
                column: 5
                delegate:Rectangle {
                    implicitWidth: 80
                    implicitHeight:30
                    TabButton{
                        id:controlactivate
                        property string colorToUse : "#b41414"
                        anchors.centerIn: parent
                        text: model.display ?"ON" :"OFF"

                        contentItem: Text {
                            text: controlactivate.text
                            opacity: enabled ? 1.0 : 0.3
                            color: (controlactivate.text === "OFF") ? "#b41414" :"#17a81a"
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            elide: Text.ElideRight
                        }

                        background: Rectangle {
                            opacity: enabled ? 1 : 0.3
                            border.color: (controlactivate.text === "OFF") ?  "#b41414" :"#17a81a"
                            border.width: 1
                            radius: 2
                        }
                        onClicked: {
                            console.log("Follow: " + model.index)
                            if(controlactivate.text === "OFF"){
                                controlactivate.text = "ON"
                            }else{
                                controlactivate.text = "OFF"
                            }
                        }
                    }
                }
            }

        }
    }
}

我有一个带有模型和委托的表格视图。当我单击特定行中的 tabButton 时,我试图将所有启用属性设置为 true 或 false,但我尝试过的所有操作都不起作用。我正在尝试使其动态化,这意味着每次单击打开或关闭按钮时,它都会启用或禁用该组件。我已经尝试过属性绑定,但听起来属性绑定不能与委托正常工作。

图像以获得预期的大图

标签: qtqml

解决方案


推荐阅读