无法分配给不存在的属性

stu*_*190 6 qt qml qtquick2

我正在尝试创建一个非常简单的程序来学习如何定义自定义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}
    }

}
Run Code Online (Sandbox Code Playgroud)

Mit*_*tch 5

Qt Quick Controls有一种Button类型,你也是.显然,Button来自Qt Quick Controls导入(没有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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)