简体   繁体   English

在子类型中增加顶级类型的 QML 属性

[英]Increment QML property of top-level type in child type

I'm new to Qt with QML.我是 Qt 和 QML 的新手。 I'm trying to set a property at the top element called buttonIndex so that the child elements could change the property by incrementing the value in the Button element to make the name go from "Device 1" -> "Device 15".我正在尝试在顶部元素上设置一个名为buttonIndex的属性,以便子元素可以通过增加 Button 元素中的值来更改属性,以使名称 go 从“设备 1”->“设备 15”。 This is my QML code below:这是我的 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
                        }
                    }
                }
            }
        }
    }
}

The output I'm getting is unwanted.我得到的 output 是不需要的。 I get "Device 107", "Device 108", "Device 109" etc.. I need to have it go from 1-15.我得到“设备 107”、“设备 108”、“设备 109”等。我需要从 1-15 获得 go。 I tried using the signal itemAdded in the code and re-assigning the value of the buttonIndex to increment by 1 each time but that didn't work either.我尝试使用代码中的信号itemAdded并重新分配buttonIndex的值以每次增加 1,但这也不起作用。 And also I need to store that buttonIndex for the Button type such like "item_device_button_1".而且我还需要为 Button 类型存储该 buttonIndex,例如“item_device_button_1”。 Any ideas how this can be solved?有什么想法可以解决这个问题吗?

As @folibis mentioned, you don't need to create your own property for this.正如@folibis 提到的,您不需要为此创建自己的财产。 Repeaters (and ListViews, etc.) give delegates access a property called index that already does exactly what you want.中继器(和 ListViews 等)使委托可以访问一个名为index的属性,该属性已经完全符合您的要求。 The index is 0-based, so if you want it to start at '1', then just add +1.索引是从 0 开始的,所以如果你希望它从 '1' 开始,那么只需添加 +1。

Repeater {
    Button {
        text: qsTr("Device ") + (index + 1)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM