Qt QML:从中继器中提取信息
在下面的代码片段中,我显示了数量可变的TextEdit项,每个项位于一个矩形中,该矩形是中继器的代表。当我点击“播放”按钮时,该槽需要收集玩家的名字,并调用QuyBooKeNosivD方法(未显示)将数据传递给C++后端。出于这个问题的目的,我只是想在Play按钮的onclick槽中显示播放器名称,但不确定如何显示。我离开了“?”,在那里我试图找出如何在每个委托实例的矩形内检索信息 在这一点上,我对这种方法的解决方案持开放态度,或者只是被告知我的方法是错误的。我对Qt的QML方面非常陌生 谢谢Qt QML:从中继器中提取信息,qt,qml,Qt,Qml,在下面的代码片段中,我显示了数量可变的TextEdit项,每个项位于一个矩形中,该矩形是中继器的代表。当我点击“播放”按钮时,该槽需要收集玩家的名字,并调用QuyBooKeNosivD方法(未显示)将数据传递给C++后端。出于这个问题的目的,我只是想在Play按钮的onclick槽中显示播放器名称,但不确定如何显示。我离开了“?”,在那里我试图找出如何在每个委托实例的矩形内检索信息 在这一点上,我对这种方法的解决方案持开放态度,或者只是被告知我的方法是错误的。我对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
}
}
}
导入QtQuick 2.12
导入QtQuick.Controls 2.12
导入QtQuick.Layouts 1.12
项目{
id:根
属性int playerCount:playerCountSelect.currentValue
行布局{
id:布局
anchors.top:parent.top
1.5页边距:5
间距:6
anchors.horizontalCenter:父级.horizontalCenter
标签{
文本:“输入玩家数量”
}
组合框{
id:playerCountSelect
型号:[2,3,4]
}
}
行布局{
id:mainRow
anchors.centerIn:父对象
anchors.horizontalCenter:父级.horizontalCenter
间距:6
圆形按钮{
文字:“播放”
宽度:40
半径:2
font.pointSize:12
再次点击:{
对于(变量i=0;i
您只需在中继器的委托中公开所需的属性
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)