【发布时间】:2021-05-19 02:05:06
【问题描述】:
我有一个页面,我想将文本按图像分开放在可滚动列表中以测试 Qt,但如果我想这样做,我必须硬定义每个图像和文本的 Y,但我不知道如何通过获得确切的 YI 必须分配的好方法来获得它。另外,我的 ScrollView 不起作用(我无法垂直滚动)。
页面:
import QtQuick 2.15
import QtQuick.Controls 2.15
Page {
id: page
width: window.width
height: window.height
title: qsTr("Text & Images")
ScrollView {
width: parent.width
height: parent.height
ListView {
Image {
source: "/images/test1.png"
anchors.horizontalCenter: parent.horizontalCenter
height: Image.height
}
Repeater {
model: 5
Label {
text: qsTr(txts["text" + (index + 1)])
anchors.horizontalCenter: parent.horizontalCenter
y: index * 400
}
Image {
source: "/images/test" + (index + 2) + ".png"
anchors.horizontalCenter: parent.horizontalCenter
y: index * 400 + 200
fillMode: Image.PreserveAspectFit
}
}
}
}
}
如果我不设置 Y,所有图像都显示在 y:0 中。
有人知道像其他语言一样自动执行此操作的组件吗?
编辑:我可以动态显示图像和文本,但滚动条不起作用并弄乱了所有 ColumnLayout。
我尝试将this 改编到我的代码中。
ColumnLayout {
anchors.fill: parent
ListView {
flickableDirection: Flickable.VerticalFlick
boundsBehavior: Flickable.StopAtBounds
Layout.fillWidth: true
Layout.fillHeight: true
ScrollBar.vertical: ScrollBar {}
Image {
source: "/images/test1.png"
}
Repeater {
model: 5
Label {
text: qsTr(txts["text" + (index + 1)])
}
Image {
source: "/images/test" + (index + 2) + ".png"
}
}
}
}
【问题讨论】:
-
改用RowLayout,设置绝对硬编码位置很奇怪。
-
Repeater 只是创建了一堆项目。您仍然需要一个定位器/布局来告诉这些项目去哪里。查看 Row/Column 或 RowLayout/ColumnLayout。
-
实际上,您是否打算将文本/图像作为 ListView 中的代表?然后完全摆脱中继器。将您的标签/图像封装在一个项目中,并将该项目分配给 ListView 的委托属性。阅读 ListView 的文档以获取示例。
-
您也不需要在
ScrollView中嵌套ListView,ListView本身也能够显示滚动条并允许滚动。 stackoverflow.com/questions/45650226/… -
RowLayout 用于动态放置它们,但@iam_peter 滚动条不像您发送的链接中的响应那样工作,我将使用当前代码更新问题