【发布时间】:2021-03-26 13:33:02
【问题描述】:
我在 QtQuick (QML) 中有以下问题。我想在表格布局中显示包含 QAbstractListModel 数据的表格。我为此使用了 GridlĹayout 和中继器:
ScrollView {
id: scrollView
width: parent.width
anchors.fill: parent
clip: true
ScrollBar.horizontal.policy: ScrollBar.AlwaysOn
ScrollBar.vertical.policy: ScrollBar.AlwaysOn
Repeater{
model: _network.qqjourneyslist.length
Item {
GridLayout {
id: inr
columns: 5
width: rect2.width
layoutDirection: "RightToLeft"
anchors.left: parent.left
anchors.top: parent.bottom
Repeater{
model: _network.qqjourneyslist[index]
Item {
Text{
id:t1
//width: 100
text: model.startstop
//Layout.fillWidth: true
}
Text{
id:t2
//width: 100
text: model.starttime
//Layout.fillWidth: true
//anchors.left: t1.right
}
Text{
id:t3
//width: 100
text: model.lineno
//Layout.fillWidth: true
//anchors.left: t2.right
}
Text{
id: t4
//width: 100
text: model.endstop
// Layout.fillWidth: true
//anchors.left: t3.right
}
Text{
id: t5
//width: 100
text: model.endtime
//Layout.fillWidth: true
//anchors.left: t4.right
}
}
}
}
}
}
}
问题是,当我在 GridLayout 中插入转发器时,流程被破坏并且文本相互覆盖。我尝试了很多方法,例如为文本插入宽度或使用 Layout.row、Layout.columns,但没有任何效果。
我还查看了Populate GridLayout with Repeater,但这个主题对我的情况没有帮助,或者至少我没有找到一种方法来为我的目的修改它。 _network.qqjourneislist 是一个带有 QAbstractListModel 的 QList,我想使用它,并且在 Layout 之外可以正常工作。
能否请您帮助我如何使用中继器和 QAbstractListModel?
我不热衷于使用 GridLayout 我只想为我的模型创建一个包含行和列的表。我知道还有很多其他表对象,但我不知道该使用哪个,并且全力以赴对我来说还有很长的路要走。
我很高兴收到的每一条建议,并感谢您!
编辑....
使用 ColumnLayout 和 RowLayout 的解决方案一直有效,直到我在中继器中使用中继器,这导致不生成文本行。
代码如下:
ColumnLayout {
id: inr
// anchors.fill: parent
width: parent.width
layoutDirection: "LeftToRight"
anchors.left: parent.left
anchors.margins: 15
//anchors.horizontalCenter: parent
anchors.top: parent.top
anchors.right: parent.right
Repeater{
model: _network.qqjourneyslist.length
Repeater{
id: rep1
model: _network.qqjourneyslist[index]
//width: inr.width
//height: 50
RowLayout{
height: 20
Layout.preferredHeight: 20
spacing: 10
Text{
id:t1
width: wind.implicitWidth/3
//anchors.left: parent.left
//anchors.top: parent.top
text: model.startstop
font.pointSize: 12
Layout.preferredWidth: wind.implicitWidth/3
}
Text{
id:t2
//width: 100
text: model.starttime
font.pointSize: 12
font.bold: true
//Layout.fillWidth: true
//anchors.left: t1.right
}
Text{
id:t3
//width: 100
text: model.lineno
font.pointSize: 12
color: "red"
//Layout.alignment: Qt.AlignVCenter
horizontalAlignment: Text.AlignHCenter
//Layout.preferredWidth: parent.implicitWidth/3
Layout.fillWidth: true
}
Text{
x:wind.implicitWidth/12*9
id: t4
text: model.endstop
Layout.alignment: Qt.AlignLeft
font.pointSize: 12
Layout.preferredWidth: parent.implicitWidth/3
//anchors.left: t3.right
}
Text{
id: t5
//width: 100
text: model.endtime
Layout.alignment: Qt.AlignRight
font.pointSize: 12
font.bold: true
//Layout.fillWidth: true
//anchors.left: t4.right
}
}
}
RowLayout{
height: 12
//Layout.preferredHeight: 12
//anchors.top: rep1.bottom
Rectangle{
//width: parent.width
//Layout.
Layout.row: 3*index
Layout.preferredHeight: 12
Layout.fillWidth: true
color: "grey"
//Layout.alignment: Qt.AlignLeft
//Layout.fillWidth: parent
//Layout.alignment: Qt.Right
radius: 5
}
}}}
我想制作类似的东西(下面的矩形)
但我只得到矩形而没有文本。如果我删除最后一个 RowLayout,则 texrows 会正确显示。
_network.qqjournieslist 是 QObejcts* 的 QList - QAbstractListModel - 因此应该可以将其分配给模型。 感谢您的每一个帮助!
【问题讨论】: