[英]Creating a new top-level QML Window using createObject (null)
[英]Increment QML property of top-level type in child type
我是 Qt 和 QML 的新手。 我正在尝试在顶部元素上设置一个名为buttonIndex的属性,以便子元素可以通过增加 Button 元素中的值来更改属性,以使名称 go 从“设备 1”->“设备 15”。 这是我的 QML 代码如下:
Page {
id: page_device
width: 1024
height: 600
property int buttonWidth: 170;
property int buttonHeight: 100;
property int buttonIndex: 1;
header: Label {
id: label_header_devices
text: qsTr("Devices")
font.pixelSize: Qt.application.font.pixelSize * 2
padding: 20
}
Item {
id: item_devices
width: 300
height: 200
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
Column {
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
spacing: 10
Repeater {
id: item_devices_rows
model: 3
Row {
spacing: 10
Repeater {
id: item_devices_columns
model: 5
Button {
id: button_device_
width: buttonWidth
height: buttonHeight
text: qsTr("Device ") + page_device.buttonIndex++
Material.foreground: Material.Pink
enabled: false
hoverEnabled: false
// onClicked: Material.background = Material.Teal
}
}
}
}
}
}
}
我得到的 output 是不需要的。 我得到“设备 107”、“设备 108”、“设备 109”等。我需要从 1-15 获得 go。 我尝试使用代码中的信号itemAdded并重新分配buttonIndex的值以每次增加 1,但这也不起作用。 而且我还需要为 Button 类型存储该 buttonIndex,例如“item_device_button_1”。 有什么想法可以解决这个问题吗?
正如@folibis 提到的,您不需要为此创建自己的财产。 中继器(和 ListViews 等)使委托可以访问一个名为index
的属性,该属性已经完全符合您的要求。 索引是从 0 开始的,所以如果你希望它从 '1' 开始,那么只需添加 +1。
Repeater {
Button {
text: qsTr("Device ") + (index + 1)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.