qt - QML - 可以在单个 View 中显示 3 个 ListView

标签 qt qml qt5 qtquick2 qt-quick

是否可以在一个页面中显示多个listView?

我有 3 个单独的 ListView ,我想将它们显示在一个页面上,但我很难对它们进行布局。它们都相互重叠。

布局的简单示例:

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
}

最佳答案

请注意,其中包含 3 个 ListViewColumn 会给您带来相当奇怪的滚动体验,您不会像滚动一样滚动它单个 View ,但滚动单个 ListView 。

只要您不对模型大小(我的意思是几千)着迷,您可以简单地使用带有 3 个中继器的 ColumnFlickable在里面。这将为您提供一个可以滚动浏览的连续列。

此解决方案对数以千计的模型项无效的原因是,在正常情况下, ListView 会根据需要创建和销毁委托(delegate),并仅保留屏幕上可见的内容以及一些可选的缓存。而此解决方案将创建所有项目,以便滚动浏览一个统一的列。

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

enter image description here

  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" }
      }
    }
  }

关于qt - QML - 可以在单个 View 中显示 3 个 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48928614/

相关文章:

QT创建者:how to change the menu font size?

android - 如何删除 QCalendarWidget 中的选定日期矩形和小部件中的小图标

C++ Qt 写入 unix 套接字

qt - QML 形状的拖动坐标

具有大量 Windows 或复杂 UI 的 Qt 5 QML 应用程序

javascript - 当 JS 代码在应用程序构建时未知时从 QML 调用 JS 函数(帽子提示 SpiderMonkey)

c++ - QInputDialog 的样式表

C++ Qt QTableWidget 项目在调整列大小时移动

c++ - 如何用 new 初始化 QVector<QVector<QString>> *matrix(作为类成员)?

c++ - Phonon 可以在 MacOS 上读取 .avi 文件吗?