scope - QML 范围 : property binding in child object failing

标签 scope qt-creator qml property-binding

我是 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

解决方案

只需将属性绑定(bind)和信号槽移动到应用程序窗口对象中即可解决,如下所示:

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

问题

为什么不能将属性绑定(bind)保留在 ApplicationWindow 对象的 Rectangle 子对象中?

如果您只想拥有矩形独有的属性(例如颜色),但使用了 ApplicationWindow 的某些属性(例如文本大小)怎么办?


我是编码和堆栈溢出的新手(这是我的第一篇文章)。我已尝试以最清晰的方式提出我的问题,但请让我知道它是否不符合堆栈溢出的标准以及我必须做些什么来改变它。

最佳答案

QML 中的范围很简单,也许很奇怪,但是很简单:

当您使用标识符时,假设在 var 绑定(bind)中说 foo,QML 引擎按以下顺序搜索:

  • 当前文件中的对象,其ID为foo
  • 全局范围(主 QML 文件)中的对象,其 ID 为 foo
  • 当前对象中的属性,称为foo
  • 当前组件(当前文件)的根对象中的一个属性,名为foo

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

不,直接父级或子级不在范围内。这看起来很奇怪,但这就是它的工作方式。

如果您需要引用范围外的变量,只需在它之前使用一个 ID:如果对象名为 foo 并且具有名为 bar 的属性,您可以在文件中任何您不想引用的地方引用 foo.bar

希望对您有所帮助。

关于scope - QML 范围 : property binding in child object failing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23338242/

相关文章:

javascript - 使用 "use strict"和可变范围

c++ - 从 Qt Creator 构建 osgEarth 应用程序

android - Android NDK 路径中的反斜杠使 QT 应用程序无法部署

c++ - QML ListView 和段错误

facebook - 如何忽略 Qt5 QML QWebKit 3.0 中的 SSL 错误?

arrays - golang 中的变量范围

javascript - 编写 meteor 包时的可变范围

objective-c - 单独在.h文件的interface()方法中定义变量而不综合它有什么区别?

调用dll函数后未捕获C++异常

qt - 如何将不透明蒙版应用于 QML 项目?