PDA

View Full Version : QTableWidget::setItem very slow the second time



miraks
14th September 2008, 17:08
Hi all,

I am trying to populate a QTableWidget with 50 lines and 50 columns with the following code:

{
SKGTRACEIN(10, "test-Filltable");
ui.kTable->clear();
ui.kTable->setRowCount(50);
ui.kTable->setColumnCount(50);
for (int i=0; i<49; ++i) {
for (int j=0; j<49; ++j) {
SKGTRACEIN(10, "test-setItem");
QTableWidgetItem* item=new QTableWidgetItem("A");
ui.kTable->setItem(i, j, item);
}
}
}
The macro SKGTRACEIN allows to measure the time consumed by the code in of the scope.

The first call of the code returns following traces:

## >test-Filltable
## >test-setItem
## <test-setItem TIME=0.100014 ms
## >test-setItem
## <test-setItem TIME=0.0169843 ms
## >test-setItem
## <test-setItem TIME=0.0140117 ms
## >test-setItem
## <test-setItem TIME=0.0148984 ms
## >test-setItem
## <test-setItem TIME=0.0140293 ms
...
...
## >test-setItem
## <test-setItem TIME=0.0139316 ms
## >test-setItem
## <test-setItem TIME=0.0148886 ms
## >test-setItem
## <test-setItem TIME=0.0138925 ms
## >test-setItem
## <test-setItem TIME=0.0150938 ms
## >test-setItem
## <test-setItem TIME=0.0150508 ms
## <test-Filltable TIME=431.761 ms
As you can see, performances are very good. Table is done in around 431ms. :D

The second call of this same code returns following traces:

## >test-Filltable
## >test-setItem
## <test-setItem TIME=27.6061 ms
## >test-setItem
## <test-setItem TIME=13.549 ms
## >test-setItem
## <test-setItem TIME=14.058 ms
## >test-setItem
## <test-setItem TIME=14.132 ms
## >test-setItem
## <test-setItem TIME=13.943 ms
...
...
## <test-setItem TIME=77.392 ms
## >test-setItem
## <test-setItem TIME=133.818 ms
## >test-setItem
## <test-setItem TIME=89.083 ms
## >test-setItem
## <test-setItem TIME=60.1529 ms
## >test-setItem
## <test-setItem TIME=61.99 ms
## <test-Filltable TIME=109696 ms
Now, performances are very bad (+25000%) and it seems to be due to setItem. :crying:

Why ? What is the solution to have good performances each time ?

Thank you in advance for your help.

jacek
14th September 2008, 19:02
What happens if you add ui.kTable->setUpdatesEnabled( ... ) around that loop? If you are concerned about the performance, use QTableView and custom model.

miraks
15th September 2008, 21:45
What happens if you add ui.kTable->setUpdatesEnabled( ... ) around that loop? If you are concerned about the performance, use QTableView and custom model.

Hi Jacek,

I didn't know this method, so thank you very much, it's interesting.

But, I still have the same issue with this code:

{
SKGTRACEIN(10, "test-Filltable");
ui.kTable->setUpdatesEnabled(false);
ui.kTable->clear();
ui.kTable->setRowCount(50);
ui.kTable->setColumnCount(50);
for (int i=0; i<49; ++i) {
for (int j=0; j<49; ++j) {
SKGTRACEIN(10, "test-setItem");
QTableWidgetItem* item=new QTableWidgetItem("A");
ui.kTable->setItem(i, j, item);
}
}
ui.kTable->setUpdatesEnabled(true);
}

I knows that I can use QTableView, but I don't want to do it each time I have a small table to display.
I did an example with 50*50 items but it's also true with 10*10 items.
I would like to understand why.

If you have an other idea ...

miraks
15th September 2008, 21:58
Hi Jacek,

I found the solution thanks to you.

I hide the table before modification, and I show it after:


{
SKGTRACEIN(10, "test-Filltable");
ui.kTable->hide();
ui.kTable->clear();
ui.kTable->setRowCount(10);
ui.kTable->setColumnCount(10);
for (int i=0; i<9; ++i) {
for (int j=0; j<9; ++j) {
SKGTRACEIN(10, "test-setItem");
QTableWidgetItem* item=new QTableWidgetItem("A");
ui.kTable->setItem(i, j, item);
}
}
ui.kTable->show();
}

With this, I always have the same performances.

Is it a bug ?

Sorry, I don't understand why, but I am not able to modify the title of my fist post to set [SOLVED].

miraks
26th September 2008, 23:18
I have a solution for a QTableWidget (hide + show), but do you know if we can have bad performances on QTableView too ?