0

当有昂贵的代表时,我在使用 ListView 时遇到了严重的问题。当我滚动列表视图时,它很生涩。

有一次请帮助解决这个问题。

这是我的示例代码

/ LIstview 带有一些昂贵的虚拟委托,我尝试使用加载器来加载委托,然后它有点好,但仍然是生涩的/

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Window 2.2

ApplicationWindow {
    visible: true
    width: Screen.width
    height: Screen.height

    ListView {
        width: Screen.width
        height: Screen.height

        model: 500
        spacing: 10

        highlightMoveVelocity: 50

        flickDeceleration: 500
        delegate: Loader{
            asynchronous: true
            sourceComponent: Image {
                width: Screen.width
                asynchronous: true
                height: index %2 === 0 ? 500: 200
                source: "file:///home/Downloads/4k.jpg"

               ///Just for show
                Image {
                    anchors.fill: parent
                    asynchronous: true
                    source: "file:///home/Downloads/4k.jpg"
                }
                Image {
                    anchors.fill: parent
                    asynchronous: true
                    source: "file:///home/Downloads/4k.jpg"
                }
                Image {
                    anchors.fill: parent
                    asynchronous: true
                    source: "file:///home/Downloads/4k.jpg"
                }
                Image {
                    anchors.fill: parent
                    asynchronous: true
                    source: "file:///home/Downloads/4k.jpg"
                }
                Image {
                    anchors.fill: parent
                    asynchronous: true
                    source: "file:///home/Downloads/4k.jpg"
                }
                Image {
                    anchors.fill: parent
                    asynchronous: true
                    source: "file:///home/Downloads/4k.jpg"
                }
                Image {
                    anchors.fill: parent
                    asynchronous: true
                    source: "file:///home/Downloads/4k.jpg"
                }

            }
        }
    }
}
4

2 回答 2

1

由于尚不清楚您的模型/代表的成本究竟是什么,因此我无法给出确切的建议,但是:

  1. 如果您必须创建一个很长的列表,其中包含需要运行一些相当昂贵的代码才能完全显示它们的项目:
    您可以在后台线程中运行昂贵的代码,然后在计算完成时更新显示。同时,您可以呈现数据的未完成版本。完成一项的计算后,您将更新显示。
    通过缓存结果可以进一步改善这一点。

  2. 随着滚动的进行,利用QAbstractListModel 的fetchMore 功能慢慢填充列表。是一个很好的例子。

当然,这两种方法都可以结合使用。
解决方案自然需要 C++,但一旦遇到 QML 的限制,这将是不可避免的。

于 2018-01-19T09:13:07.290 回答
1

也许尝试增加cacheBuffer属性。请记住 - 该值是您要预加载的像素。

除此之外,精简委托是要走的路,但这需要您的实际用例,而不仅仅是一些昂贵的虚拟委托

于 2018-01-19T08:53:13.213 回答