下面的代码在列和行中使用 ListView。但是 ListView 不可滚动。知道为什么吗?
import QtQuick 2.0
import Ubuntu.Components 0.1
MainView {
width: units.gu(60)
height: units.gu(60)
Page {
id: test
Column {
spacing: units.gu(1)
id: pageLayout
anchors {
margins: units.gu(2)
fill: parent
}
Row {
id: buttonRow
spacing: units.gu(1)
Button {
objectName: "button1"
color: "white"
text: i18n.tr("Help")
}
Button {
objectName: "button2"
color: "black"
text: i18n.tr("Search")
}
}
Row {
id: listarea
spacing: units.gu(1)
anchors.top: buttonRow.bottom
height: myList.count * units.gu(2)
ListModel {
id: fruitModel
ListElement {
name: "Apple"
cost: 2.45
}
ListElement {
name: "Orange"
cost: 3.25
}
ListElement {
name: "Banana"
cost: 1.95
}
}
ListView {
id: myList
anchors.fill: parent
model: fruitModel
delegate: Row {
Text { text: "Fruit: " + name }
Text { text: "Cost: $" + cost }
}
}
}
}
}
}
答案1
Row
在这个版本中,我只用一个元素替换了最后一个Item
。
我还添加了一个Scrollbar
组件:
Scrollbar {
flickableItem: list
align: Qt.AlignTrailing
}
作为列表显示继承Flickable
,添加Scrollbar
可实现垂直滚动。
import QtQuick 2.0
import Ubuntu.Components 0.1
MainView {
width: units.gu(60)
height: units.gu(60)
ListModel {
id: fruitModel
ListElement {
name: "Apple"
cost: 2.45
}
ListElement {
name: "Orange"
cost: 3.25
}
ListElement {
name: "Banana"
cost: 1.95
}
}
Page {
id: test
Column {
spacing: units.gu(1)
id: pageLayout
anchors {
margins: units.gu(2)
fill: parent
}
Row {
id: buttonRow
spacing: units.gu(1)
Button {
objectName: "button1"
color: "white"
text: i18n.tr("Help")
}
Button {
objectName: "button2"
color: "black"
text: i18n.tr("Search")
}
}
Item {
anchors.top: buttonRow.bottom
ListView {
id: list
width: units.gu(18)
height: units.gu(3)
model: fruitModel
boundsBehavior: Flickable.StopAtBounds
delegate: Row {
Text { text: "Fruit: " + name }
Text { text: "Cost: $" + cost }
}
}
Scrollbar {
flickableItem: list
align: Qt.AlignTrailing
}
}
}
}
}