Results 1 to 5 of 5

Thread: QTableWidget::setItem very slow the second time

  1. #1
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QTableWidget::setItem very slow the second time

    Hi all,

    I am trying to populate a QTableWidget with 50 lines and 50 columns with the following code:
    Qt Code:
    1. {
    2. SKGTRACEIN(10, "test-Filltable");
    3. ui.kTable->clear();
    4. ui.kTable->setRowCount(50);
    5. ui.kTable->setColumnCount(50);
    6. for (int i=0; i<49; ++i) {
    7. for (int j=0; j<49; ++j) {
    8. SKGTRACEIN(10, "test-setItem");
    9. ui.kTable->setItem(i, j, item);
    10. }
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. ## >test-Filltable
    2. ## >test-setItem
    3. ## <test-setItem TIME=0.100014 ms
    4. ## >test-setItem
    5. ## <test-setItem TIME=0.0169843 ms
    6. ## >test-setItem
    7. ## <test-setItem TIME=0.0140117 ms
    8. ## >test-setItem
    9. ## <test-setItem TIME=0.0148984 ms
    10. ## >test-setItem
    11. ## <test-setItem TIME=0.0140293 ms
    12. ...
    13. ...
    14. ## >test-setItem
    15. ## <test-setItem TIME=0.0139316 ms
    16. ## >test-setItem
    17. ## <test-setItem TIME=0.0148886 ms
    18. ## >test-setItem
    19. ## <test-setItem TIME=0.0138925 ms
    20. ## >test-setItem
    21. ## <test-setItem TIME=0.0150938 ms
    22. ## >test-setItem
    23. ## <test-setItem TIME=0.0150508 ms
    24. ## <test-Filltable TIME=431.761 ms
    To copy to clipboard, switch view to plain text mode 
    As you can see, performances are very good. Table is done in around 431ms.

    The second call of this same code returns following traces:
    Qt Code:
    1. ## >test-Filltable
    2. ## >test-setItem
    3. ## <test-setItem TIME=27.6061 ms
    4. ## >test-setItem
    5. ## <test-setItem TIME=13.549 ms
    6. ## >test-setItem
    7. ## <test-setItem TIME=14.058 ms
    8. ## >test-setItem
    9. ## <test-setItem TIME=14.132 ms
    10. ## >test-setItem
    11. ## <test-setItem TIME=13.943 ms
    12. ...
    13. ...
    14. ## <test-setItem TIME=77.392 ms
    15. ## >test-setItem
    16. ## <test-setItem TIME=133.818 ms
    17. ## >test-setItem
    18. ## <test-setItem TIME=89.083 ms
    19. ## >test-setItem
    20. ## <test-setItem TIME=60.1529 ms
    21. ## >test-setItem
    22. ## <test-setItem TIME=61.99 ms
    23. ## <test-Filltable TIME=109696 ms
    To copy to clipboard, switch view to plain text mode 
    Now, performances are very bad (+25000%) and it seems to be due to setItem.

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

    Thank you in advance for your help.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget::setItem very slow the second time

    What happens if you add ui.kTable->setUpdatesEnabled( ... ) around that loop? If you are concerned about the performance, use QTableView and custom model.

  3. The following user says thank you to jacek for this useful post:

    miraks (15th September 2008)

  4. #3
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableWidget::setItem very slow the second time

    Quote Originally Posted by jacek View Post
    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:
    Qt Code:
    1. {
    2. SKGTRACEIN(10, "test-Filltable");
    3. ui.kTable->setUpdatesEnabled(false);
    4. ui.kTable->clear();
    5. ui.kTable->setRowCount(50);
    6. ui.kTable->setColumnCount(50);
    7. for (int i=0; i<49; ++i) {
    8. for (int j=0; j<49; ++j) {
    9. SKGTRACEIN(10, "test-setItem");
    10. ui.kTable->setItem(i, j, item);
    11. }
    12. }
    13. ui.kTable->setUpdatesEnabled(true);
    14. }
    To copy to clipboard, switch view to plain text mode 

    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 ...

  5. #4
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default [SOLVED] QTableWidget::setItem very slow the second time

    Hi Jacek,

    I found the solution thanks to you.

    I hide the table before modification, and I show it after:
    Qt Code:
    1. {
    2. SKGTRACEIN(10, "test-Filltable");
    3. ui.kTable->hide();
    4. ui.kTable->clear();
    5. ui.kTable->setRowCount(10);
    6. ui.kTable->setColumnCount(10);
    7. for (int i=0; i<9; ++i) {
    8. for (int j=0; j<9; ++j) {
    9. SKGTRACEIN(10, "test-setItem");
    10. ui.kTable->setItem(i, j, item);
    11. }
    12. }
    13. ui.kTable->show();
    14. }
    To copy to clipboard, switch view to plain text mode 

    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].
    Last edited by miraks; 15th September 2008 at 21:06.

  6. #5
    Join Date
    Aug 2008
    Posts
    70
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableWidget::setItem very slow the second time

    I have a solution for a QTableWidget (hide + show), but do you know if we can have bad performances on QTableView too ?

Similar Threads

  1. How to constantly refresh time on a view
    By salmanmanekia in forum Qt Programming
    Replies: 5
    Last Post: 23rd June 2008, 12:44
  2. Replies: 1
    Last Post: 1st February 2008, 18:55
  3. QDateTime GMT add sec. or - sec. from locale time....
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2007, 16:39
  4. Replies: 1
    Last Post: 20th January 2006, 12:01
  5. Problem with pointers while using localtime() and time()
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 11th January 2006, 15:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.