2

I have a QtTableWidget with 7 columns and a lot of rows (something like 2000, but will grow to 5000 and probably even more).

This data is retrieved from a database. The query and the post processing took less than 100ms, so this is not the problem.

But inserting the items at the table take a lot of time (something like 30 seconds) for 7x2000 items.

I tried to optimize the code as much as I could, but I don't know to do anymore.

Also, I must note that when the application is launched, this same table, with the same data set is loaded and very very fast. So I believe this is a rendering issue.

Searching at the docs for something to block the rendering, I came up with the setUpdatesEnabled method, which is not helping at all.

The following is the code I'm using to build the table:

setUpdatesEnabled(false);
blockSignals(true);
bool sort = isSortingEnabled();
setSortingEnabled(false);
// rebuild headers.
buildHeaders(); // take less than 1ms.
// rebuild grid.
int oldRowCount = rowCount();
setRowCount(sml->length()); // take less than 1ms.
ListIterator<StoredMetadata> it(sml); // sml is a list with the data to be displayed.
int row = 0;
while ( it.hasNext() ) {
    AudioLife::StoredMetadata *sm = it.next();
    if ( row >= oldRowCount ) {
        setItem(row, 0, new QTableWidgetItem(sm->artist));
        setItem(row, 1, new QTableWidgetItem(sm->album));
        setItem(row, 2, new QTableWidgetItem(sm->name));
        setItem(row, 3, new QTableWidgetItem(sm->number > 0 ? QString().setNum(sm->number) : ""));
        setItem(row, 4, new QTableWidgetItem(sm->year > 0 ? QString().setNum(sm->year) : ""));
        setItem(row, 5, new QTableWidgetItem(sm->genre));
        // commenting the following line does not improve performance.
        verticalHeader()->resizeSection(row, defaultRowHeight());
    } else {
        item(row, 0)->setText(sm->artist);
        item(row, 1)->setText(sm->album);
        item(row, 2)->setText(sm->name);
        item(row, 3)->setText(sm->number > 0 ? QString().setNum(sm->number) : "");
        item(row, 4)->setText(sm->year > 0 ? QString().setNum(sm->year) : "");
        item(row, 5)->setText(sm->genre);
    }
    item(row, 0)->setData(Qt::UserRole, QVariant(sm->id));
    row++;
}
setUpdatesEnabled(true);
blockSignals(false);
setSortingEnabled(sort);

Does anybody have any clue?

  • This is a simple hack setUniformRowHeights(true); this will gain you almost 50% time .i am not sure why :) – Midhun Aug 31 '16 at 16:48

1 Answers1

2

Consider subclassing QSqlTableModel or QSqlQueryModel for your table model and let the framework do the item display for you, instead of manually insert/set items of the widget

[Edit]: Or directly use QSqlTableModel as your table model

Ref: Using-QSqlTableModel-To-Display-an-SQL-view, QSQLTableModel inheritor and QTableView, and some sample code here

Community
  • 1
  • 1
YamHon.CHAN
  • 866
  • 4
  • 20
  • 36
  • 1
    I could not find any decent tutorial on how to extend these classes. So I architectured my project with the Q*Widget classes. If you could point to me any good tutorial on that, I will be very happy to rewrite all the needed code and project details. Qt render this thing fast some way... maybe I'll take a look at the sources of QSqlTableModel. – Vinícius Gobbo A. de Oliveira May 29 '13 at 11:50