qt - 有没有办法在 QML 的网格中重复一行?
问题描述
我是 QML 的新手。Note
您可以通过替换和FixedNote
来重现以下示例Button
。我试图让最后一行重复可变次数。当用户单击“添加和弦”时,我想添加最后一行的另一个重复,当用户单击“删除和弦”时,我想添加删除最后一行。我的直觉(最后一行的嵌套中继器)似乎不起作用?
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
Window {
visible: true
width: 640
height: 480
title: qsTr("Justly")
ScrollView {
anchors.fill: parent
Column {
spacing: 20
Row {
anchors.horizontalCenter: parent.horizontalCenter
spacing: 5
TextField {
anchors.verticalCenter: parent.verticalCenter
placeholderText: qsTr("100")
width: 50
}
Text {
anchors.verticalCenter: parent.verticalCenter
text: "bpm"
}
Text {
anchors.verticalCenter: parent.verticalCenter
text: "with key starting at"
}
TextField {
anchors.verticalCenter: parent.verticalCenter
placeholderText: qsTr("440")
width: 60
}
Text {
anchors.verticalCenter: parent.verticalCenter
text: "hz"
}
}
Row {
spacing: 20
anchors.horizontalCenter: parent.horizontalCenter
Grid {
id: grid
columns: 3
spacing: 15
anchors.verticalCenter: parent.verticalCenter
horizontalItemAlignment: Grid.AlignHCenter
verticalItemAlignment: Grid.AlignVCenter
Text {
text: "Chord"
}
Text {
text: "Key"
}
Repeater{
id: voice_names
model: 1
TextField {
placeholderText: model.index + 1
width: 70
}
}
Button {
text: "Play"
}
FixedNote { }
Repeater {
id: voice_notes
model: 1
Note { }
}
}
Column {
spacing: 5
anchors.verticalCenter: parent.verticalCenter
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "Add voice"
onClicked: {
grid.columns = grid.columns + 1
voice_names.model = voice_names.model + 1
voice_notes.model = voice_notes.model + 1
}
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "Remove voice"
onClicked: {
voice_names.model = voice_names.model - 1
voice_notes.model = voice_notes.model - 1
grid.columns = grid.columns - 1
}
}
}
}
Column {
spacing: 5
anchors.horizontalCenter: parent.horizontalCenter
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "Add chord"
onClicked: {
// chords.model = chords.model + 1
}
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "Remove chord"
onClicked: {
// chords.model = chords.model - 1
}
}
}
}
}
}
解决方案
据我了解,您正在尝试创建一个TextField
项目矩阵。你的直觉是对的,你可以使用两个嵌套的中继器。一个生成行,另一个生成列。
这是一个如何做到这一点的示例。有两个属性来保存行数和列数,然后将此属性用作中继器的模型:
import QtQuick 2.12
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
Item {
anchors.fill: parent
anchors.margins: 30
property int numRows: 4
property int numCols: 5
Column {
spacing: 10
Text {
height: 50
text: "QML input matrix demo"
font.bold: true
font.pixelSize: 20
}
Row {
spacing: 10
Text {
width: 70
text: " "
}
Repeater {
model: numCols
delegate: Text {
width: 70
text: "Col " + index
}
}
}
Repeater {
model: numRows
delegate: Row {
spacing: 10
property int rIndex: index
Text {
width: 70
anchors.verticalCenter: parent.verticalCenter
text: "Row " + index
}
Repeater {
model: numCols
delegate: TextField {
property int cIndex: index
width: 70
text: rIndex + "," + cIndex
}
}
}
}
Item {
height: 50
width: 1
}
Row {
spacing: 5
Button {
text: "Add col"
onClicked: numCols = numCols + 1
}
Button {
text: "Remove col"
onClicked: numCols = numCols - 1
}
Button {
text: "Add row"
onClicked: numRows++
}
Button {
text: "Remove row"
onClicked: numRows--
}
}
}
}
这也是该片段的工作版本: https ://cutes.io/project/Kg
推荐阅读
- python - 如何使用批量插入将数据从 Dataframe 插入 SQL Server 表?
- jenkins - 如何配置 Jenkins 拉取请求构建器以在 Github 依赖 PR 上运行测试
- python - 计算一个函数被调用了多少次
- neo4j - 得到
Neo4j 节点的分类 - java - 如何使状态视图突出显示和超链接链接
- pytorch - Huggingface BERT SequenceClassification - ValueError:要解包的值太多(预期为 2)
- coq - 如果 Coq 中两个归纳类型的构造函数表达式相等,我可以根据它们对应的参数进行重写吗?
- php - 使用 CURL 和代理发送 POST 请求
- c++ - 如何在 SDL2 中使用鼠标突出显示文本(例如,在 msword 中,您可以单击并突出显示文本。)
- system-verilog - 如何在没有额外信号的情况下减少左值总线?