首页 > 解决方案 > 为什么在 QML TableView 的 syncView 上设置implicitHeight 会大大降低其性能?

问题描述

我想创建一个简单TableView的使用另一个来使用属性TableView显示其标题。syncView我将标题布局在列中的主要内容上方:

import QtQuick 2.14
import QtQuick.Window 2.12
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import Qt.labs.qmlmodels 1.0

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    ColumnLayout {
        anchors.fill: parent

        TableView {
            Layout.fillWidth: true
            id: headerView
            implicitHeight: contentHeight   //comment this line out and it'll be much faster

            model: TableModel {
                TableModelColumn {display: "index"}
                rows: [{index: "index"}]
            }

            delegate: Label {
                text: model.display
            }
        }

        TableView {
            Layout.fillWidth: true
            Layout.fillHeight: true
            syncView: headerView
            syncDirection: Qt.Horizontal

            model: TableModel {
                TableModelColumn {display: "index"}
                rows: Array(100000).fill().map((_, index) => ({index}))
            }

            delegate: Label {
                text: model.display
            }

            ScrollBar.vertical: ScrollBar {}
        }
    }
}

不幸的是,设置非零值implicitHeightheaderView导致性能变慢。这可以通过快速上下拖动垂直滚动条来感受。这里发生了什么?我使用syncView错误的方式吗?

标签: qtqmlqt5qtquick2

解决方案


推荐阅读