我是使用 QML 的 Qt 新手。我正在嘗試在頂部元素設定一個名為buttonIndex的屬性,以便子元素可以通過增加 Button 元素中的值來更改屬性,使名稱從“設備 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
}
}
}
}
}
}
}
我得到的輸出是不需要的。我得到“設備 107”、“設備 108”、“設備 109”等。我需要讓它從 1 到 15。我嘗試在代碼中使用信號itemAdded并重新分配buttonIndex的值以每次增加 1 但這也不起作用。而且我還需要為 Button 型別(例如“item_device_button_1”)存盤那個 buttonIndex。任何想法如何解決?
uj5u.com熱心網友回復:
正如@folibis 提到的,您不需要為此創建自己的屬性。中繼器(和 ListViews 等)讓代表訪問一個名為的屬性index,該屬性已經完全符合您的要求。該索引是從 0 開始的,因此如果您希望它從 '1' 開始,那么只需添加 1。
Repeater {
Button {
text: qsTr("Device ") (index 1)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/378508.html
