【发布时间】: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
}
}
}
【问题讨论】: