我正在尝试创建一个非常简单的程序来学习如何定义自定义QML类型以供重用.我不知道为什么我收到以下错误:
无法分配给不存在的属性"颜色"
我已经找到了答案,但没有找到解决问题的方法.
下面是代码.Qt的强调color和radius红色,这意味着它被标记为"无效属性的名称."
//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)
Qt Quick Controls有一种Button类型,你也是.显然,Button来自Qt Quick Controls导入(没有radius或color属性)来自本地文件.你有几个选择:
Button类型重命名为其他内容.以下是您选择#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)
| 归档时间: |
|
| 查看次数: |
10727 次 |
| 最近记录: |