【问题标题】:Cannot assign to non-existent property无法分配给不存在的属性
【发布时间】:2015-12-07 00:19:13
【问题描述】:

我正在尝试制作一个非常简单的程序来学习如何定义自定义 QML 类型以供重用。我不确定为什么会出现以下错误:

不能分配给不存在的属性“颜色”

我已经搜索了一个答案,但没有找到任何解决它的方法。

下面是代码。 Qt 用红色下划线colorradius,表示它被标记为“无效的属性名称”。

//Button.qml
import QtQuick 2.3

Rectangle {
width: 100; height: 100
color: "red"

    MouseArea {
        anchors.fill: parent
        onClicked: console.log("button clicked!")
    }
}

//main.qml 
import QtQuick 2.3
import QtQuick.Controls 1.2

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

    menuBar: MenuBar {
        Menu {
            title: qsTr("File")
            MenuItem {
                text: qsTr("&Open")
                onTriggered: console.log("Open action triggered");
            }
            MenuItem {
                text: qsTr("Exit")
                onTriggered: Qt.quit();
            }
        }
    }

    Column {
        Button {width: 50; height: 50}
        Button { x: 50; width: 100; height: 50; color: "blue" }
        Button { width: 50; height: 50; radius: 8}
    }

}

【问题讨论】:

    标签: qt qml qtquick2


    【解决方案1】:

    Qt Quick Controls 有一个Button 类型,你也一样。显然,来自 Qt Quick Controls 导入(没有 radiuscolor 属性)的 Button 被选择而不是本地文件。您有几个选择:

    1. 将您的 Button 类型重命名为其他名称。
    2. 将 Qt Quick Controls 导入命名空间。
    3. 将您的类型导入命名空间。

    选项 #2 的方法如下:

    import QtQuick 2.3
    import QtQuick.Controls 1.2 as Controls
    
    Controls.ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        menuBar: Controls.MenuBar {
            Controls.Menu {
                title: qsTr("File")
                Controls.MenuItem {
                    text: qsTr("&Open")
                    onTriggered: console.log("Open action triggered")
                }
                Controls.MenuItem {
                    text: qsTr("Exit")
                    onTriggered: Qt.quit()
                }
            }
        }
    
        Column {
            Button {
                width: 50
                height: 50
            }
            Button {
                x: 50
                width: 100
                height: 50
                color: "blue"
            }
            Button {
                width: 50
                height: 50
                radius: 8
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 2017-07-11
      • 1970-01-01
      相关资源
      最近更新 更多