无法分配给不存在的 属性

Cannot assign to non-existent property

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

Cannot assign to non-existent property "color"

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

下面是代码。 Qt 用红色下划线 colorradius,这意味着它被标记为 "invalid property name."

//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 Quick Controls 有一个 Button 类型,你也有。显然,Qt Quick Controls 导入中的 Button(没有 radiuscolor 属性)在您的本地文件中被选中。您有几个选择:

  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
        }
    }
}