在QML中滚动两个或更多列表视图

时间:2017-08-29 14:03:29

标签: qt listview qml qtquick2 flickable

我需要使用单个scrollBar 一次滚动两个或更多列表视图。最初,我在Column中使用Flickable,但滚动未按预期发生。后来,我使用了ListView,甚至没有正确滚动。

那么如何使用滚动条滚动列表视图/布局内容项?我应该使用ScrollView还是Flickable还是别的?

Example of My UI

2 个答案:

答案 0 :(得分:3)

股票滚动条只会挂钩到一个可滚动的项目。但是,创建一个自定义滚动条并将多个视图挂钩到它是微不足道的:

  Row {
    Flickable {
      width: 50
      height: main.height
      contentHeight: contentItem.childrenRect.height
      interactive: false
      contentY: (contentHeight - height) * scroller.position
      Column {
        spacing: 5
        Repeater {
          model: 20
          delegate: Rectangle {
            width: 50
            height: 50
            color: "red"
            Text {
              anchors.centerIn: parent
              text: index
            }
          }
        }
      }
    }
    Flickable {
      width: 50
      height: main.height
      contentHeight: contentItem.childrenRect.height
      interactive: false
      contentY: (contentHeight - height) * scroller.position
      Column {
        spacing: 5
        Repeater {
          model: 30
          delegate: Rectangle {
            width: 50
            height: 50
            color: "cyan"
            Text {
              anchors.centerIn: parent
              text: index
            }
          }
        }
      }
    }
    Rectangle {
      id: scroller
      width: 50
      height: 50
      color: "grey"
      property real position: y / (main.height - 50)
      MouseArea {
        anchors.fill: parent
        drag.target: parent
        drag.minimumY: 0
        drag.maximumY: main.height - 50
        drag.axis: Drag.YAxis
      }
    }
  }

请注意,即使视图具有不同的内容高度,它也能正常工作,相对于滚动条位置滚动每个视图:

enter image description here

意识到这个问题并没有那么好,以防万一有人想要在同一时间实际滚动多个视图,我仍然会分享另一种类似于滚轮的有趣方法,这种方法可以无限期地向各个方向发展而不是像滚动条那样有限的范围。此解决方案将同步滚动两个视图,直到它们达到其范围的范围。与GrecKo的答案不同,这绝不会让你看到一个空洞的观点。当视图大小不同时:

enter image description here

  Row {
    Flickable {
      id: f1
      width: 50
      height: main.height
      contentHeight: contentItem.childrenRect.height
      interactive: false
      Connections {
        target: jogger
        onScroll: f1.contentY = Math.max(0, Math.min(f1.contentHeight - f1.height, f1.contentY + p))
      }
      Column {
        spacing: 5
        Repeater {
          model: 20
          delegate: Rectangle {
            width: 50
            height: 50
            color: "red"
            Text {
              anchors.centerIn: parent
              text: index
            }
          }
        }
      }
    }
    Flickable {
      id: f2
      width: 50
      height: main.height
      contentHeight: contentItem.childrenRect.height
      interactive: false
      Connections {
        target: jogger
        onScroll: f2.contentY = Math.max(0, Math.min(f2.contentHeight - f2.height, f2.contentY + p))
      }
      Column {
        spacing: 5
        Repeater {
          model: 30
          delegate: Rectangle {
            width: 50
            height: 50
            color: "cyan"
            Text {
              anchors.centerIn: parent
              text: index
            }
          }
        }
      }
    }
    MouseArea {
      id: jogger
      width: 50
      height: main.height
      drag.target: knob
      drag.minimumY: 0
      drag.maximumY: main.height - 50
      drag.axis: Drag.YAxis
      signal scroll(real p)
      property real dy: 0
      onPressed: dy = mouseY
      onPositionChanged: {
        scroll(dy - mouseY)
        dy = mouseY
      }
      onScroll: console.log(p)
      Rectangle {
        anchors.fill: parent
        color: "lightgrey"
      }
      Rectangle {
        id: knob
        visible: parent.pressed
        width: 50
        height: 50
        color: "grey"
        y: Math.max(0, Math.min(parent.mouseY - 25, parent.height - height))
      }
    }
  }

" jog"的另一个优点方法是不是相对而是绝对的。这意味着如果您的视图很大,如果使用滚动条,即使单个像素也可能导致内容发生重大变化,而在绝对模式下工作的慢跑将始终滚动相同数量的像素,而不管内容大小如何,这是方便需要精确度的地方。

答案 1 :(得分:2)

您可以在Flickable上使用Columns。 我不知道你的Columns是如何水平布局的,但如果它们在Row内,那么它非常简单:

import QtQuick 2.7
import QtQuick.Controls 2.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Multi Column")

    Flickable {
        anchors.fill: parent
        contentWidth: row.implicitWidth
        contentHeight: row.implicitHeight
        Row {
            id: row
            Column {
                spacing: 5
                Repeater {
                    model: 20
                    delegate: Rectangle {
                        width: 50
                        height: 50
                        color: "red"
                        Text {
                            anchors.centerIn: parent
                            text: index
                        }
                    }
                }
            }
            Column {
                spacing: 5
                Repeater {
                    model: 30
                    delegate: Rectangle {
                        width: 50
                        height: 50
                        color: "cyan"
                        Text {
                            anchors.centerIn: parent
                            text: index
                        }
                    }
                }
            }
        }
        ScrollBar.vertical: ScrollBar { }
    }
}

即使他们不在Row,你也可以这样做:
contentHeight: Math.max(column1.height, column2.height, ...)

示范:
Multi columns scroll