Results 1 to 6 of 6

Thread: Slow loading of data into QTableWidget

  1. #1
    Join Date
    Nov 2011
    Posts
    30
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Slow loading of data into QTableWidget

    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



    Qt Code:
    1. void InsertRow(int iRow)
    2. {
    3.  
    4. tableDataWidget->insertRow(iRow);
    5.  
    6. // Colum 0 -- Row number
    7. TableItem = new QTableWidgetItem(QString::number(iRow) );
    8. tableDataWidget->setItem(iRow,0,TableItem);
    9.  
    10. // Colum 1 -- Sample Type
    11. aString = Data->GetDataBaseItemLabel(SampleType);
    12. aString += "-";
    13. aString += QString::number(SampleType);
    14. TableItem = new QTableWidgetItem(aString);
    15. tableDataWidget->setItem(iRow,1,TableItem);
    16.  
    17. // Colum 2 -- Sample Time
    18. ThisTime.setTime_t(SampleTakenTime);
    19. TableItem = new QTableWidgetItem(ThisTime.toString(Qt::SystemLocaleShortDate) );
    20.  
    21. // Colum 3 -- Sample Load Time
    22. tableDataWidget->setItem(iRow,2,TableItem);
    23. ThisTime.setTime_t(SampleLoadedTime );
    24.  
    25. // Colum 4 -- This Time
    26. TableItem = new QTableWidgetItem(ThisTime.toString(Qt::SystemLocaleShortDate) );
    27. tableDataWidget->setItem(iRow,3,TableItem);
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Slow loading of data into QTableWidget

    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.

  3. #3
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Slow loading of data into QTableWidget

    To increase performanse when inserting many rows at a time use setRowCount() instead of insertRow().

  4. #4
    Join Date
    Nov 2011
    Posts
    30
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: Slow loading of data into QTableWidget

    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

  5. #5
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Slow loading of data into QTableWidget

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

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Slow loading of data into QTableWidget

    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.

Similar Threads

  1. Best practice when handling a slow loading widget?
    By TheNewGuy in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2009, 06:12
  2. QTableWidget::setItem very slow the second time
    By miraks in forum Qt Programming
    Replies: 4
    Last Post: 26th September 2008, 22:18
  3. Slow loading images
    By abbapatris in forum Qt Programming
    Replies: 10
    Last Post: 5th March 2008, 15:52
  4. QTableWidget Update - slow
    By DPinLV in forum Qt Programming
    Replies: 16
    Last Post: 18th August 2006, 21:09
  5. Adding QComboBox to QTableWidget very slow
    By munna in forum Qt Programming
    Replies: 2
    Last Post: 13th July 2006, 15:45

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.