为什么条件绑定导致“未定义”的绑定?

时间:2018-09-10 15:34:26

标签: qt qml

我试图实现Master-Detail概念,其中Detail取决于当前值。绑定禁用细节时出现问题-它仍然处于活动状态。我使用了条件绑定,但没有太大帮助。我仍然以某种方式获得“未定义”的价值。当我在Binding中添加“延迟”属性时,它不会改变任何东西,甚至无法被QtCreator识别。

有什么想法吗?

main.qml:

import QtQuick 2.11
import QtQuick.Controls 2.4
import QtQuick.Window 2.11
import QtQuick.Layouts 1.3

Window {
    id: root

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


    property var value: {
        if(detailSwitch.checked) {
            return {
                "budget": 5000,
                "payable": false,
                "type": "FormA"
            }
        } else {
            return {
                "description": "form B",
                "accepted": false,
                "type": "FormB"
            }
        }
    }


    ColumnLayout {
        spacing: 0
        anchors.fill: parent

        Switch {
            id: detailSwitch
            Layout.fillWidth: true
            Layout.preferredHeight: 300

            anchors.horizontalCenter: parent.horizontalCenter
        }

        Flickable {
            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.preferredHeight: 1

            contentHeight: contentItem.childrenRect.height
            clip: true

            flickableDirection: Flickable.VerticalFlick
            ScrollIndicator.vertical: ScrollIndicator {}

            FormA {
                id: formA

                enabled: root.value.type === "FormA"
                visible: enabled

                Binding on value {
                    when: formA.enabled
                    value: root.value
                }
            }

            FormB {
                id: formB

                enabled: root.value.type === "FormB"
                visible: enabled

                Binding on value {
                    when: formB.enabled
                    value: root.value
                }
            }
        }
    }
}

FormA:

import QtQuick 2.11
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3

GridLayout {
    id: root

    // Defaults
    property var value : {
        "budget": 7000,
        "payable": true
    }

    width: parent.width
    columns: 3
    columnSpacing: 5

    Label {
        text: "Budget"
        font.pointSize: 16
    }
    Item { Layout.fillWidth: true }
    TextField {
        Layout.alignment: Qt.AlignRight

        text: root.value.budget
        validator: IntValidator {}
    }

    Label {
        text: "Payable"
        font.pointSize: 16
    }
    Item { Layout.fillWidth: true }
    Switch {
        Layout.alignment: Qt.AlignRight

        checked: root.value.payable
        rightPadding: 0
    }
}

FormB:

import QtQuick 2.11
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3

GridLayout {
    id: root

    // Defaults
    property var value : {
        "description": "Description placeholder",
        "accepted": true
    }

    width: parent.width
    columns: 3
    columnSpacing: 5

    Label {
        text: "Description"
        font.pointSize: 16
    }
    Item { Layout.fillWidth: true }
    TextField {
        Layout.alignment: Qt.AlignRight

        text: root.value.description
        validator: IntValidator {}
    }

    Label {
        text: "Accepted"
        font.pointSize: 16
    }
    Item { Layout.fillWidth: true }
    CheckBox {
        Layout.alignment: Qt.AlignRight

        checked: root.value.accepted
        rightPadding: 0
    }
}

0 个答案:

没有答案