【问题标题】:QML - Possible to display 3 listViews in a single viewQML - 可以在单个视图中显示 3 个列表视图
【发布时间】:2018-02-22 13:27:49
【问题描述】:

是否可以在一个页面中显示多个列表视图?

我有 3 个单独的列表视图,我想将它们显示在一个页面上,但我正在努力布置它们。它们都相互重叠。

简单的布局示例:

ListView {
    id: listOne
    property string headertitle: "list one header"
    spacing: 5
    header: headerComponent
    model: ListOneModel
}

ListView {
    id: listTwo
    property string headertitle: "list two header"
    spacing: 5
    header: headerComponent
    model: ListTwoModel
}

ListView {
    id: listThree
    property string headertitle: "list three header"
    spacing: 5
    header: headerComponent
    model: ListThreeModel
}

【问题讨论】:

  • 是的,是可能的。如果您希望我们为您提供帮助,您必须提供minimal reproducible example,并指明您希望它们在页面上的放置方式。
  • 您希望它们如何显示:全部在列中还是在一行中?
  • @eyllanesc 3 行,全宽,每行 1 个列表视图。

标签: qt qml qt5 qtquick2 qt-quick


【解决方案1】:

请注意,其中包含 3 个 ListViews 的 Column 会给您带来相当奇怪的滚动体验,您不会像滚动单个视图一样滚动它,而是滚动单个列表视图。

只要您对模型尺寸(我的意思是数千个)不感兴趣,您就可以简单地使用带有 3 个中继器的 FlickableColumn。这将为您提供一个可以滚动浏览的连续列。

此解决方案对数千个模型项无效的原因是,在正常情况下,列表视图会根据需要创建和销毁委托,并且只创建屏幕上可见的内容以及一些可选的缓存。而此解决方案将创建所有项目,以便获得一个统一的列来滚动。

下面是一个简单的例子,展示了两种不同的行为:

  Row {
    anchors.fill: parent
    Flickable {
      width: parent.width * .5
      height: parent.height
      flickableDirection: Flickable.VerticalFlick
      contentHeight: contentItem.childrenRect.height
      Column {
        spacing: 2
        Repeater {
          model: 5
          delegate: Rectangle { width: 100; height: 100; color: "red" }
        }
        Repeater {
          model: 5
          delegate: Rectangle { width: 100; height: 100; color: "green" }
        }
        Repeater {
          model: 5
          delegate: Rectangle { width: 100; height: 100; color: "blue" }
        }
      }
    }
    Column {
      width: parent.width * .5
      height: parent.height
      spacing: 2
      ListView {
        spacing: 2
        width: parent.width
        height: parent.height / 3
        model: 5
        clip: true
        delegate: Rectangle { width: 100; height: 100; color: "red" }
      }
      ListView {
        spacing: 2
        width: parent.width
        height: parent.height / 3
        model: 5
        clip: true
        delegate: Rectangle { width: 100; height: 100; color: "green" }
      }
      ListView {
        spacing: 2
        width: parent.width
        height: parent.height / 3
        model: 5
        clip: true
        delegate: Rectangle { width: 100; height: 100; color: "blue" }
      }
    }
  }

【讨论】:

  • 这很棒。感谢您提供这两种行为。我可以在我的应用中看到两者的使用情况。
【解决方案2】:

你可以使用Column:

import QtQuick 2.0

Rectangle {
    width: 180; height: 200
    id: root

    Column {
        ListView {
            width: root.width; height: root.height/3
            model: myModel
            delegate: Text {
                text: name + ": " + number
            }
        }

        ListView {
            width: root.width; height: root.height/3

            model: myModel
            delegate: Text {
                text: name + ": " + number
            }
        }

        ListView {
            width: root.width; height: root.height/3

            model: myModel
            delegate: Text {
                text: name + ": " + number
            }
        }
    }

    ListModel {
        id: myModel
        ListElement {
            name: "Bill Smith"
            number: "555 3264"
        }
        ListElement {
            name: "John Brown"
            number: "555 8426"
        }
        ListElement {
            name: "Sam Wise"
            number: "555 0473"
        }
    }

}

【讨论】:

  • 这是一个可滚动的列表。 OP 要求多个并行列表视图......因此,另一个答案得到了我的赞成票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多