PDA

View Full Version : Slow loading of data into QTableWidget



KenJustKen
8th January 2012, 03:17
I am running into a performance issue loading data into a QTableWidget when only loading 100-300 rows. It can take 10-20 seconds to load this data so I have to be doing someing wrong. Below is a snipet of code that is called for every row. I normally load 8 -9 columns but only the first 5 columns are shown below. All data is text based. No graphics or anything else. This is new Tablewidget which has no data in it at the beginning and I am just loading the table with data. Any suggestions on what my problem is?

Ken




void InsertRow(int iRow)
{

tableDataWidget->insertRow(iRow);

// Colum 0 -- Row number
TableItem = new QTableWidgetItem(QString::number(iRow) );
tableDataWidget->setItem(iRow,0,TableItem);

// Colum 1 -- Sample Type
aString = Data->GetDataBaseItemLabel(SampleType);
aString += "-";
aString += QString::number(SampleType);
TableItem = new QTableWidgetItem(aString);
tableDataWidget->setItem(iRow,1,TableItem);

// Colum 2 -- Sample Time
ThisTime.setTime_t(SampleTakenTime);
TableItem = new QTableWidgetItem(ThisTime.toString(Qt::SystemLocal eShortDate) );

// Colum 3 -- Sample Load Time
tableDataWidget->setItem(iRow,2,TableItem);
ThisTime.setTime_t(SampleLoadedTime );

// Colum 4 -- This Time
TableItem = new QTableWidgetItem(ThisTime.toString(Qt::SystemLocal eShortDate) );
tableDataWidget->setItem(iRow,3,TableItem);

}

Lykurg
8th January 2012, 08:22
You can try to disable signal emitting before starting inserts and activate them afterwards (blockSignals). You can do that as well with the model.

The best performance you will get, if you directly deal with a view and a - custom? - Model.

Spitfire
9th January 2012, 12:23
To increase performanse when inserting many rows at a time use setRowCount() instead of insertRow().

KenJustKen
11th April 2012, 15:33
By looking to see if there was space in my table, and if there wasn't I used a setRowCount to add 10 more rows and then started adding the data until these 10 rows were done and then repeated. This greatly increased the speed! Is this what you meant?

ken

Spitfire
11th April 2012, 15:47
More or less :)
I ment that If you know how much data you want to add then setting row count in one go is very efficient. There's no need to do it in steps if you know how big the whole batch is.

Your approach is ok, just remember that after adding last item you need to check if there are any empty rows left and remove them (unless they don't bother you).

d_stranz
11th April 2012, 15:50
It would be even faster if you know the total number of rows in advance, instead of allocating them 10 at a time. Call setRowCount() once with the total count, and you're done.

If you don't now know the total in advance, then it would probably help a lot if you restructured your code so that you could determine the count first. Break it up so you retrieve your table data from wherever first, then pass the whole thing to the table. Sounds like you're retrieving data at the same time you are filling the table, which seems like an inappropriate mixing of two logically separate functions.