向QML小部件添加滚动条(Qt 5.9.3)
我需要在向QML小部件添加滚动条(Qt 5.9.3),qt,scroll,qml,scrollbar,Qt,Scroll,Qml,Scrollbar,我需要在文本编辑中添加垂直滚动,在列表视图中添加水平滚动,必要时显示滚动条。这两个小部件都必须填满它们的父布局提供的整个空间。我该怎么做?例子对我没有帮助 ScrollableTextEdit: ColumnLayout { ... ScrollView { id: scroll_view Layout.fillWidth: true Layout.fillHeight: true ScrollBar.horizon
文本编辑中添加垂直滚动,在列表视图中添加水平滚动,必要时显示滚动条。这两个小部件都必须填满它们的父布局提供的整个空间。我该怎么做?例子对我没有帮助
ScrollableTextEdit
:
ColumnLayout {
...
ScrollView {
id: scroll_view
Layout.fillWidth: true
Layout.fillHeight: true
ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
???
Rectangle {
border.color: 'gray'
???
TextEdit {
id: text_edit
anchors.fill: parent
textFormat: TextEdit.RichText
wrapMode: TextEdit.Wrap
}
}
}
}
ScrollableListView
:
ColumnLayout {
...
ScrollView {
id: scroll_view
Layout.fillWidth: true
Layout.fillHeight: true
ScrollBar.vertical.policy: ScrollBar.AlwaysOff
???
Rectangle {
border.color: 'gray'
???
ListView {
id: list_view
anchors.fill: parent
...
}
}
}
}
您有太多的项目层。ScrollView应该是TextEdit的直接父级。对于ListView,可以使用附加的ScrollBar.horizontal属性创建滚动条,而不使用ScrollView
见下面的例子
ApplicationWindow {
visible: true
x: 0
y: 0
width: 600
height: 200
id: root
RowLayout
{
anchors.fill: parent
anchors.margins: 20
ColumnLayout
{
Label
{
text: "Example 1 (Vertical TextEdit)"
}
ScrollView
{
id: textEdit
Layout.fillHeight: true
Layout.fillWidth: true
clip: true
TextEdit
{
text: "A very\nvery\nvery\nvery\nvery\nvery\nvery\nvery\nvery\nvery\nvery\nvery\nvery\nvery\nvery\nvery\nvery long text"
anchors.fill: parent
}
}
}
ColumnLayout
{
Label
{
text: "Example 2 (Horizontal ListView)"
}
ListView
{
id: listView
Layout.fillHeight: true
Layout.fillWidth: true
model: 10
spacing: 10
orientation: ListView.Horizontal
ScrollBar.horizontal: ScrollBar {}
delegate: Rectangle {
border.width: 1
height: parent.height
width: 100
Text {
anchors.centerIn: parent
text: index
}
}
}
}
}
}