Qml 将GridLayout与QabStretchModelList和中继器一起使用
我在QtQuick(QML)中有以下问题。我想在表格布局中显示一个包含QAbstractListModel数据的表格。我使用GridlĹ布局和中继器:Qml 将GridLayout与QabStretchModelList和中继器一起使用,qml,repeater,qt-quick,Qml,Repeater,Qt Quick,我在QtQuick(QML)中有以下问题。我想在表格布局中显示一个包含QAbstractListModel数据的表格。我使用GridlĹ布局和中继器: ScrollView { id: scrollView width: parent.width anchors.fill: parent clip: true ScrollBar.horizontal.policy: ScrollBar.AlwaysOn
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,但都没有效果。我也看了,但这个主题对我的情况没有帮助,或者至少我没有找到一种方法来修改它_network.qqjourneislist是一个QList,带有QabStretchListModel,我想使用它,在布局之外,它可以正常工作。 您能帮助我如何使用中继器和QabStretchListModel吗?
我不喜欢使用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
}
}}}
我想制作这样的东西(下面的矩形)
但我只得到矩形,没有文本。如果删除最后一行布局,则会正确显示texrows
_network.qqjournieslist是QObejcts*-QAbstractListModel的一个QList,因此应该可以将它分配给model。
谢谢你的帮助 对象会覆盖它们自己,因为它们的父对象是一个
项
,它不会告诉它们要定位在哪里。您并没有真正描述希望输出的外观,因此我假设您希望这5个文本字符串在网格中显示为一行。然后,您可以将列
与行
一起使用,(或者如果愿意,将列布局
与行布局
一起使用)
编辑:
您更新的代码不起作用,因为您似乎误解了中继器的工作方式。中继器的模型可以是它应该重复多少次的计数,也可以是某种类型的列表,告诉中继器要创建多少项
您的第一个中继器
使用列表的长度作为模型,这是正常的。假设它的值是10。这意味着它将创造10个里面的任何东西。那中继器里面是什么?另一个中继器
。第二个中继器的型号是列表中单个项目的内容。这是个问题。除非该项也是某种类型的列表,中继器将不知道如何将其用作计数。这就是为什么在我上面的代码中,我将您的示例更改为只使用一个中继器。你的案子就需要这些了。每行的模型需要重复,但列不需要重复。但如果文本不相同,它们将占用不同的空间,对吗?我认为海报需要一个表格视图,其中所有项目都在彼此下方整齐排列。您可以为每个文本
对象指定宽度。是的,但如果您希望应用程序具有多个分辨率,则不太好是的,但这并不是一项要求。使用RowLayout
可以解决这个问题。我本来应该使用网格布局来制作网格,这是否意味着对于需要重新绘制的表,我不能使用网格或网格布局?你的解决方案有效,但我认为有一个更优雅的理由,为什么要这样做。
Column {
Row {
Repeater {
model: _network.qqjourneyslist
Text {
width: 100 // Column 1 width
text: model.startstop
}
Text {
width: 100 // Column 2 width
text: model.starttime
}
...
}
}
}