Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QHeaderView::ResizeToContents在QTableView中用于垂直标头的性能问题_C++_Qt - Fatal编程技术网

C++ QHeaderView::ResizeToContents在QTableView中用于垂直标头的性能问题

C++ QHeaderView::ResizeToContents在QTableView中用于垂直标头的性能问题,c++,qt,C++,Qt,如果QTableView显示一个自定义项目模型,其中只有一列但有数百行,那么将垂直标题的部分resizemode设置为ResizeToContents会对性能产生巨大的负面影响 出于调试原因,我在项目模型的::data方法中添加了一些输出,以查看视图实际查询了哪些行。事实证明,表格视图在需要渲染时,只要将resize mode设置为ResizeToContents,实际上就会查询模型中的每一行,而不管显示多少行 下面的代码示例给出了以下输出: *** show *** query 0 quer

如果
QTableView
显示一个自定义项目模型,其中只有一列但有数百行,那么将垂直标题的
部分resizemode
设置为
ResizeToContents
会对性能产生巨大的负面影响

出于调试原因,我在项目模型的
::data
方法中添加了一些输出,以查看视图实际查询了哪些行。事实证明,表格视图在需要渲染时,只要将resize mode设置为
ResizeToContents
,实际上就会查询模型中的每一行,而不管显示多少行

下面的代码示例给出了以下输出:

*** show ***
query  0
query  1
query  2
query  3
  ... many lines trimmed ...
query  495
query  496
query  497
query  498
query  499
query  0
query  1
query  2
query  3
  ... many lines trimmed ...
query  495
query  496
query  497
query  498
query  499
query  0
query  1
query  2
query  3
query  4
query  5
query  6
query  0
query  1
query  2
query  3
query  4
query  5
query  6
也就是说,视图首先似乎在所有行上迭代两次。然后,它迭代表视图视口中实际可见的行。恰巧在我的屏幕上,有七行可见

注释掉兴趣线后,示例的输出减少为:

*** show ***
query  0
query  1
query  2
query  3
query  0
query  1
query  2
query  3
由于这些行现在的默认高度比以前稍大,因此现在只有四行可见。更重要的是,现在从模型中总共只查询了八行

为什么会有这种奇怪的行为

SCCE
scce.pro

QT += core gui widgets
CONFIG += c++11
TARGET = sscce
TEMPLATE = app
SOURCES += main.cpp
main.cc

#include <QAbstractItemModel>
#include <QApplication>
#include <QHeaderView>
#include <QTableView>
#include <QDebug>

class Model: public QAbstractItemModel {
    public:
        int rowCount(const QModelIndex &parent) const {
            return parent.isValid() ? 0 : 500;
        }

        int columnCount(const QModelIndex &parent) const {
            return parent.isValid() ? 0 : 1;
        }

        QModelIndex parent(const QModelIndex &/* child */) const {
            return QModelIndex();
        }

        QModelIndex index(int row, int column, const QModelIndex &parent) const {
            return parent.isValid() ? QModelIndex() : createIndex(row, column, Q_NULLPTR);
        }

        QVariant data(const QModelIndex &index, int role) const {
            if (role == Qt::DisplayRole)
                qDebug() << "query " << index.row();

            return (index.isValid() && (role == Qt::DisplayRole)) ?
                QStringLiteral("Row %1").arg(index.row()) : QVariant();
        }

        QVariant headerData(int section, Qt::Orientation orientation, int role) const {
            return ((orientation == Qt::Vertical) && (role == Qt::DisplayRole)) ? section : QVariant();
        }
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QTableView view;
    view.setModel(new Model());

    /* Line of interest: */
    view.verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

    qDebug() << "*** show ***";
    view.show();
    return app.exec();
}
#包括
#包括
#包括
#包括
#包括
类模型:公共QabstracteModel{
公众:
int行计数(常量QModelIndex和父级)常量{
返回parent.isValid()?0:500;
}
int columnCount(常量QModelIndex&parent)常量{
返回parent.isValid()?0:1;
}
QModelIndex父(常量QModelIndex&/*子*/)常量{
返回QModelIndex();
}
QModelIndex索引(int行、int列、常量QModelIndex&parent)常量{
返回parent.isValid()?QModelIndex():createIndex(行、列、Q_NULLPTR);
}
QVariant数据(常量QModelIndex和索引,int角色)常量{
如果(角色==Qt::DisplayRole)

qDebug()
ResizeToContents
为所有行设置相同的高度,甚至设置最适合所有行的高度,因此必须查询整个表

如果行是恒定的,并且您能够手动确定它们的高度,您可以尝试存储所需的最小高度,然后安装事件过滤器以调整大小并手动设置高度。其他情况类似,但处理起来更复杂(添加/删除/编辑行等时必须连接到信号)