QTableView慢性能与1000s的可见细胞 [英] QTableView slow performance with 1000s of visible cells

查看:210
本文介绍了QTableView慢性能与1000s的可见细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Qt 4.8.4中使用QTableView可视化很多数据(大/许多蛋白质氨基酸序列),我想能够使细胞尽可能小,所以我可以打包尽可能多可能进入给定的窗口。我遇到的问题是,当有许多单元格同时显示,一切(例如滚动,调整大小,一般重绘)减慢到爬行。下面是一些示例代码(改编自examples / tutorials / 1_readonly教程):

I'm using QTableView in Qt 4.8.4 to visualize a lot of data (large/many protein amino acid sequences) and I'd like to be able to make cells as small as possible so I can pack as many as possible into a given window. The problem I'm running into is that when there are many cells displayed at once, everything (e.g. scrolling, resizing, and in general repainting) slows down to a crawl. Here's some sample code (adapted from the examples/tutorials/1_readonly tutorial):

MyModel::MyModel(QObject *parent):QAbstractTableModel(parent){}

int MyModel::rowCount(const QModelIndex & /*parent*/) const {
   return 200;
}
int MyModel::columnCount(const QModelIndex & /*parent*/) const {
    return 60;
}
QVariant MyModel::data(const QModelIndex &index, int role) const {
    if (role == Qt::DisplayRole){
       return QString("%1").arg(index.row()%10);
    }
    return QVariant();
}

这里是运行表视图的代码:

and here's the code which runs the table view:

int main(int argc, char *argv[]){
   QApplication a(argc, argv);
   QTableView tableView;    
   tableView.horizontalHeader()->setDefaultSectionSize(15);
   tableView.verticalHeader()->setDefaultSectionSize(15);
   tableView.setFont(QFont("Courier",12));
   MyModel myModel(0);
   tableView.setModel( &myModel );
   tableView.setGeometry(0,0,1000,1000);
   tableView.show();
   return a.exec();
}

当我在OSX上使用Instruments时向上和向下滚动,的时间在 QWidgetPrivate :: drawWidget 并向下堆栈, QWidgetPrivate :: paintSiblingsRecursive ... ie,很多时候重绘我的表。

When I use Instruments on OSX while scrolling up and down, it's spending a lot of time in QWidgetPrivate::drawWidget and down the stack, QWidgetPrivate::paintSiblingsRecursive... i.e., it's spending a lot of time redrawing my table.

我是新的Qt,所以我不知道如何解决这个问题。应该:

I'm new to Qt, so I'm not sure how to approach this problem. Should I:


  • 是否覆盖绘图方法?

  • 不使用表格(例如,我可以将整个表格保存为图像),当滚动发生时,只需重新绘制图像,直到移动停止在Qt?也许我可以只使用一个文本字段来完成我的目的?例如

这两个选项看起来像是通过切换远离QTableView,很多工作弥补地面损失。还有其他建议吗?

Both of these options seem like a lot of work to make up for ground lost by switching away from QTableView. Are there any other suggestions?

推荐答案

QTableView在处理大型数据集时很慢。建议您切换到 Qt图形视图框架

QTableView is known to be slow when dealing with large datasets. I suggest you to switch to Qt Graphics View Framework. It's much more efficient and is flexible enough to display a table.

QGraphicsScene scene;
QFont font("Courier",12);
QFontMetrics font_metrics(font);
int padding = 2;
int column_width = font_metrics.width("X") + padding * 2;
int row_height = font_metrics.height() + padding * 2;
int rows = 200, columns = 60;
for(int x = 0; x < columns; x++) {
  for(int y = 0; y < rows; y++) {
    QGraphicsSimpleTextItem* item = scene.addSimpleText(QString().setNum(y % 10), font);
    item->setPos(x * column_width + padding, y * row_height + padding);
  }
}
for(int x = 0; x < columns + 1; x++) {
  int line_x = x * column_width;
  scene.addLine(line_x, 0, line_x, rows * row_height)->setPen(QPen(Qt::gray));
}
for(int y = 0; y < rows + 1; y++) {
  int line_y = y * row_height;
  scene.addLine(0, line_y, columns * column_width, line_y)->setPen(QPen(Qt::gray));
}
QGraphicsView view(&scene);
view.resize(700, 700);
view.show();

这篇关于QTableView慢性能与1000s的可见细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆