首页 文章

QML范围:子对象中的属性绑定失败

提问于
浏览
3

我是QML的新手,在阅读按钮教程时遇到了范围问题 . 我解决了它,但我不明白为什么代码首先不起作用:

问题

当按钮悬停在以下代码时,以下代码会给出运行时引用错误:

main_broken.qml

import QtQuick 2.0
    import QtQuick.Controls 1.1

    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Button Tester")

        Rectangle {
                id: simpleButton
                height: 75
                width: 150
                property color buttonColor: "light blue"
                property color onHoverColor: "gold"
                property color borderColor: "white"

                onButtonClick: {
                        console.log(buttonLabel.text + " clicked")
                }

                signal buttonClick()



                Text {
                    id: buttonLabel
                    anchors.centerIn: parent
                    text: "button label"
                }

                MouseArea {
                    id: buttonMouseArea
                    anchors.fill: parent
                    onClicked: buttonClick()
                    hoverEnabled: true
                    onEntered: parent.border.color = onHoverColor
                    onExited: parent.border.color = borderColor
                }

                color: buttonMouseArea.pressed ? Qt.darker(buttonColor, 1.5) : buttonColor
                scale: buttonMouseArea.pressed ? 0.99 : 1
        }

    }

错误:

qrc:///main.qml:37: ReferenceError: onHoverColor is not defined
qrc:///main.qml:38: ReferenceError: borderColor is not defined
qrc:///main.qml:37: ReferenceError: onHoverColor is not defined
qrc:///main.qml:35: ReferenceError: buttonClick is not defined

解决方案

通过将属性绑定和信号槽移动到Application窗口对象来解决,如下所示:

main_fixed.qml

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Button Tester")

    property color buttonColor: "light blue"
    property color onHoverColor: "gold"
    property color borderColor: "white"

    onButtonClick: {
            console.log(buttonLabel.text + " clicked")
    }

    signal buttonClick()

    //etc

问题

为什么不能将属性绑定留在ApplicationWindow对象的Rectangle子节点中?

如果您想要仅具有矩形(例如颜色)的属性,但是使用了ApplicationWindow的某些属性(例如文本大小),该怎么办?


我是编码和堆栈溢出的新手(这是我的第一篇文章) . 我试图以最清晰的方式提出我的问题,但如果不符合堆栈溢出的标准以及我必须做些什么来改变它,请告诉我 .

1 回答

  • 4

    QML中的范围很简单,可能很奇怪,但很简单:

    当你使用标识符时,让我们在var绑定中说 foo ,QML引擎按此顺序搜索:

    • urrent file urrent file 中有 foo 作为其ID

    • global scope (主要QML文件) foo 作为其ID

    • a property in current object ,名为 foo
      当前组件(当前文件)的 root object

    • a property ,名为 foo

    如果它找到它,它会抛出 ReferenceError .

    不,直接的父母或孩子是 not in the scope . 这看起来很奇怪,但这就是它的工作方式 .

    如果需要引用超出范围的变量,只需在它之前使用ID:如果对象名为 foo 并且具有名为 bar 的属性,则可以在文件中的任何位置引用 foo.bar .

    希望能帮助到你 .

相关问题