qml中垂直Listview内的水平listView
我想让一个水平列表视图作为另一个垂直列表视图的委托,我编写了以下代码:
import Qt 4.7
Item {
id:main
width: 360
height: 640
Component{
id:myDelegate
ListView{
id:list2
spacing: 5
width:list.width
height:list.height/3
interactive: true
orientation: ListView.Horizontal
model: ListModel {
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "John Brown"
number: "555 8426"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
}
delegate: Text{text:name
width: main.width/3}
focus: true
MouseArea {
anchors.fill: parent
onClicked: {
ListView.list2.currentIndex = ListView.list2.indexAt(mouseX, mouseY)
}
}
}
}
ListView {
id: list
clip: true
spacing: 5
anchors.fill: parent
orientation: ListView.Vertical
model: Model{}
delegate:myDelegate
// highlight: Rectangle {
// width: list.currentItem.width
// color: "lightsteelblue"
// radius: 5
// }
focus: true
MouseArea {
anchors.fill: parent
onClicked: {
list.currentIndex = list.indexAt(mouseX, mouseY)
}
}
}
}
垂直列表视图滚动良好,但水平列表视图不滚动。 有什么帮助吗? 谢谢
I want to make an horizontal listView works as a delegate for another veritcal listView, I've wrote the following code:
import Qt 4.7
Item {
id:main
width: 360
height: 640
Component{
id:myDelegate
ListView{
id:list2
spacing: 5
width:list.width
height:list.height/3
interactive: true
orientation: ListView.Horizontal
model: ListModel {
ListElement {
name: "Bill Smith"
number: "555 3264"
}
ListElement {
name: "John Brown"
number: "555 8426"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
ListElement {
name: "Sam Wise"
number: "555 0473"
}
}
delegate: Text{text:name
width: main.width/3}
focus: true
MouseArea {
anchors.fill: parent
onClicked: {
ListView.list2.currentIndex = ListView.list2.indexAt(mouseX, mouseY)
}
}
}
}
ListView {
id: list
clip: true
spacing: 5
anchors.fill: parent
orientation: ListView.Vertical
model: Model{}
delegate:myDelegate
// highlight: Rectangle {
// width: list.currentItem.width
// color: "lightsteelblue"
// radius: 5
// }
focus: true
MouseArea {
anchors.fill: parent
onClicked: {
list.currentIndex = list.indexAt(mouseX, mouseY)
}
}
}
}
The vertical listview scroll well but the horizontal one does not scroll.
Any help?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我尝试了一次,但没有成功,外部列表处理所有事件。解决方案是在 ListView 之外添加一个 Flickable,并将水平列表的 contentX 和垂直列表的 contentY 锚定到 Flickable 的 contentX 和 contentY。
一些半完整的代码来展示原理:
I tried it once and it didn't work, the outer list handles all events. The solution was to have a Flickable additionally to the ListViews and anchor the contentX of the horizontal and contentY of the vertical lists to contentX and contentY of the Flickable.
Some semi complete code to show the principle:
我在模拟器上尝试了这个解决方案并且它有效。
I tried this solution on the simulator and it worked.
您的垂直列表视图中有一个
MouseArea
,它将所有事件窃取到您的水平列表视图。 QML 中的最佳实践是将所有MouseArea
组件包含在委托内。另外,不要使用
indexAt(mouseX,mouseY)
方法,而是使用所有委托都可用的index
属性。为了将鼠标事件从列表委托的
MouseArea
传播到列表委托的MouseArea
,请使用mouse.accepted = false
You have a
MouseArea
in your vertical list view which steals all events to your horizontal ListView. Best practice in QML is to include allMouseArea
components inside the delegate.Also, instead of using the
indexAt(mouseX,mouseY)
method, use theindex
property available to all delegates.In order to propogate the mouse event from list delegate's
MouseArea
to list2 delegate'sMouseArea
, usemouse.accepted = false
您可以使用
z
属性使外部列表首先处理鼠标事件。或者甚至更好:
但不太确定这是一种可靠且正确的方法。
You can use
z
property to make outer lists handle mouse events first.Or even better:
Not quite sure this is a reliable and proper way though.