【问题标题】:QML: Extract information from repeaterQML:从转发器中提取信息
【发布时间】:2020-12-11 23:49:21
【问题描述】:

在下面代码的 sn-p 中,我显示了可变数量的 TextEdit 项目,每个项目都在一个 Rectangle 中,它是转发器的代表。当我点击“播放”按钮时,该插槽需要收集玩家姓名并调用 Q_INVOKABLE 方法(未显示)将该数据传递到 C++ 后端。出于这个问题的目的,我只是想在播放按钮的 onClicked 插槽中显示播放器名称,但不确定如何。我离开了 '???'我试图弄清楚如何在每个委托实例的矩形内检索信息。

在这一点上,我对这种方法的解决方案持开放态度,或者只是被告知我以错误的方式接近这种方法。我对 Qt 的 QML 方面真的很陌生。

谢谢

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12

Item {
    id: root
    property int playerCount: playerCountSelect.currentValue

    RowLayout {
        id: layout
        anchors.top: parent.top
        anchors.topMargin: 5
        spacing: 6
        anchors.horizontalCenter: parent.horizontalCenter

        Label {
            text: "Enter number of players"
        }

        ComboBox {
            id: playerCountSelect
            model: [2, 3, 4]
        }
    }

    RowLayout {
        id: mainRow
        anchors.centerIn: parent
        anchors.horizontalCenter: parent.horizontalCenter
        spacing: 6

        RoundButton {
            text: "Play"
            width: 40
            radius: 2
            font.pointSize: 12
            onClicked: {
                for (var i =0; i < playerList.count; i++) {
                    console.log(playerList.itemAt(i).???)
                }
            }
        }

        Column {
            spacing: 6
            Repeater {
                id: playerList
                model: root.playerCount

                Rectangle {
                    anchors.horizontalCenter: parent.horizontalCenter
                    width: 120
                    height: 32

                    TextEdit {
                        font.pointSize: 12
                        text: "Player " + (index+1) + " name"
                    }
                }
            }
        }

        RoundButton {
            text: "Quit"
            width: 40
            radius: 2
            font.pointSize: 12
        }
    }
}

【问题讨论】:

    标签: qt qml


    【解决方案1】:

    您只需要在中继器的委托中公开您想要的属性。

                Repeater {
                    id: playerList
                    model: root.playerCount
    
                    Rectangle {
                        anchors.horizontalCenter: parent.horizontalCenter
                        width: 120
                        height: 32
    
                        // Expose the player name
                        property alias playerName: textField.text
    
                        TextEdit {
                            id: textField
                            font.pointSize: 12
                            text: "Player " + (index+1) + " name"
                        }
                    }
                }
    

    然后您可以通过执行以下操作在您的打印语句中访问该属性:

    console.log(playerList.itemAt(i).playerName)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-07
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多