英文:
How to have Text that wraps, has a maximum height, and becomes scrollable if the text goes over that maxiumum height?
问题
这种方式基本可以实现你的需求,但是Text组件没有在窗口高度减小时变为可滚动,而且ListView的顶部与Text的底部不对齐。
如果要解决这些问题,你可以使用Flickable组件来包装Text,并设置Flickable的高度为窗口高度的1/3。这将使文本在高度超过1/3窗口高度时可滚动。此外,可以通过设置ListView的anchors.top为descriptionBox.bottom来确保ListView的顶部与Text的底部对齐。
以下是修改后的代码示例:
ColumnLayout {
anchors.fill: parent
Flickable {
id: descriptionBox
clip: true
height: parent.height / 3
contentWidth: parent.width
flickableDirection: Flickable.VerticalFlick
Text {
id: descriptionBoxLabel
text: "Eum id neque possimus inventore similique. Et dolores exercitationem vel dignissimos. Voluptatibus assumenda veniam consequuntur. Harum reprehenderit tempora nostrum. Assumenda unde omnis non sit minima voluptas eligendi eum."
width: parent.width
wrapMode: Text.WordWrap
}
}
ListView {
id: useListView
model: ["Apple", "Banana", "Pear", "Watermelon"]
delegate: CheckDelegate {
text: modelData
width: Window.width - 20
}
Layout.fillWidth: true
Layout.fillHeight: true
anchors.top: descriptionBox.bottom
}
}
这个修改后的代码应该满足你的需求,使Text可滚动,并确保ListView的顶部与Text的底部对齐。
英文:
I'm trying to set up a Text component that has a maximum height of the window's height divided by three. The source of the text gives it as a single line, so it also needs to wrap once its width exceeds that of the window. Finally, I'd like the text to become scrollable if its height exceeds 1/3 of the window's.
I've also got a ListView below it, and I want the top of the ListView to be right at the bottom of the Text.
So far, I've come up with this:
ColumnLayout {
anchors.fill: parent
// anchors.margins: 10
ScrollView {
id: descriptionBox
clip: true
Layout.fillWidth: true
Layout.preferredHeight: parent.height / 3
contentWidth: parent.width
Text {
id: descriptionBoxLabel
text: "Eum id neque possimus inventore similique. Et dolores exercitationem vel dignissimos. Voluptatibus assumenda veniam consequuntur. Harum reprehenderit tempora nostrum. Assumenda unde omnis non sit minima voluptas eligendi eum."
anchors.left: parent.left
anchors.right: parent.right
wrapMode: Text.WordWrap
}
}
ListView {
id: useListView
model: ["Apple", "Banana", "Pear", "Watermelon"]
delegate: CheckDelegate {
text: modelData
width: Window.width - 20
}
Layout.fillWidth: true
Layout.fillHeight: true
}
}
This kind of works, but the Text doesn't become scrollable when I shrink the height of the window, and the top of the ListView isn't lined up with the bottom of the Text.
答案1
得分: 1
我不确定原问题是什么,但是我重新修改了它,使用Flickable而不是ScrollView。区别在于你需要仔细设置ScrollBar的contentWidth和contentHeight以使其正常工作。我还调整了你的ListView委托的宽度计算,使其跟随ListView的宽度:
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
ColumnLayout {
anchors.fill: parent
Flickable {
id: descriptionBox
Layout.fillWidth: true
Layout.preferredHeight: Math.min(parent.height / 3, frame.height)
clip: true
contentWidth: frame.width
contentHeight: frame.height
ScrollBar.vertical: ScrollBar {
width: 20
policy: ScrollBar.AlwaysOn
}
Frame {
id: frame
width: descriptionBox.width - 20
Text {
id: descriptionBoxLabel
text: "Eum id neque possimus inventore similique. Et dolores exercitationem vel dignissimos. Voluptatibus assumenda veniam consequuntur. Harum reprehenderit tempora nostrum. Assumenda unde omnis non sit minima voluptas eligendi eum."
width: parent.width
wrapMode: Text.WordWrap
}
}
}
ListView {
id: useListView
Layout.fillWidth: true
Layout.fillHeight: true
model: ["Apple", "Banana", "Pear", "Watermelon"]
delegate: CheckDelegate {
text: modelData
width: ListView.view.width - 20
}
ScrollBar.vertical: ScrollBar {
width: 20
policy: ScrollBar.AlwaysOn
}
}
}
}
你可以在这里在线尝试
【编辑】
为了消除空白空间,使用以下代码修改Layout的高度:
Layout.preferredHeight: Math.min(parent.height / 3, frame.height)
英文:
I am not sure what the original issue is, but, I reworked it to use a Flickable instead of a ScrollView. The difference being you have to be meticulous in setting contentWidth and contentHeight correctly for the ScrollBar to work. I also tweaked the width calculation of your ListView delegate to follow the width of the ListView:
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
ColumnLayout {
anchors.fill: parent
Flickable {
id: descriptionBox
Layout.fillWidth: true
Layout.preferredHeight: Math.min(parent.height / 3, frame.height)
clip: true
contentWidth: frame.width
contentHeight: frame.height
ScrollBar.vertical: ScrollBar {
width: 20
policy: ScrollBar.AlwaysOn
}
Frame {
id: frame
width: descriptionBox.width - 20
Text {
id: descriptionBoxLabel
text: "Eum id neque possimus inventore similique. Et dolores exercitationem vel dignissimos. Voluptatibus assumenda veniam consequuntur. Harum reprehenderit tempora nostrum. Assumenda unde omnis non sit minima voluptas eligendi eum."
width: parent.width
wrapMode: Text.WordWrap
}
}
}
ListView {
id: useListView
Layout.fillWidth: true
Layout.fillHeight: true
model: ["Apple", "Banana", "Pear", "Watermelon"]
delegate: CheckDelegate {
text: modelData
width: ListView.view.width - 20
}
ScrollBar.vertical: ScrollBar {
width: 20
policy: ScrollBar.AlwaysOn
}
}
}
}
You can Try it Online!
[EDIT]
To eliminate the white space, modified the Layout height with:
Layout.preferredHeight: Math.min(parent.height / 3, frame.height)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论