Results 1 to 17 of 17

Thread: Too slow adding & defining items to QTreeWidget

  1. #1
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Too slow adding & defining items to QTreeWidget

    WinXP
    QT4.0.1

    The code below fills a QTreeWidget with items from a table in my database. I set the text so users can identify the name and set the WhatsThis to the id of the row so I know which one was selected by the user.

    I'll take any advice to speed this up in any way. The table can have anywhere from 10-2000 entries. If i remove the text, WhatsThis, and icon it speeds things up by a factor of 10. This is really kicking my butt. Any help would be greatly appreciated.

    Qt Code:
    1. query.exec( "SELECT name,number,id,identifier FROM table;" );
    2.  
    3. while( query.next() )
    4. {
    5. List << new QTreeWidgetItem(sectorsNode);
    6. qString.sprintf("%s | %d" , query.value(0).toByteArray().data(), query.value(1).toInt() );
    7. List .last()->setText( 0, qString );
    8. List .last()->setWhatsThis( 0, query.value(2).toString() );
    9. List .last()->setIcon( 0, Icon );
    10. QApplication::processEvents();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jnk5y; 11th May 2006 at 20:47.

  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: Too slow adding & defining items to QTreeWidget

    Try this:
    Qt Code:
    1. setUpdatesEnabled( false );
    2. query.exec( ... );
    3. while( query.next() ) {
    4. // ...
    5. }
    6. setUpdatesEnabled( true );
    To copy to clipboard, switch view to plain text mode 

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

    jnk5y (11th May 2006)

  4. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Too slow adding & defining items to QTreeWidget

    - construct items without a parent (and store a pointer to the newly created item)
    - access the item (to set text, whatsthis and icon) through the pointer, not by indexing the list
    - add all items to a list
    - add the list of items as children to the correct parent

    Something like this:
    Qt Code:
    1. QList<QTreeWidgetItem*> items;
    2. while(...)
    3. {
    4. QTreeWidgetItem* item = new QTreeWidgetItem; // don't pass a parent here!
    5. ...
    6. item->setText(...);
    7. item->setWhatsThis(...);
    8. item->setIcon(...);
    9. items << item;
    10. }
    11. sectorsNode->addChildren(items);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. The following 2 users say thank you to jpn for this useful post:

    foxyproxy (3rd April 2008), jnk5y (11th May 2006)

  6. #4
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Too slow adding & defining items to QTreeWidget

    jacek - Thank you. That did the trick!

    jpn - I see what you are saying and yes that will definitely improve speed but I QTreeWidgetItem has no addChildren function only addChild

  7. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Too slow adding & defining items to QTreeWidget

    Quote Originally Posted by jnk5y
    QTreeWidgetItem has no addChildren function only addChild
    Right, seems that it was introduced in Qt 4.1. My bad..
    J-P Nurmi

  8. #6
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Too slow adding & defining items to QTreeWidget

    Don't worry about it jpn

    Actually just creating a pointer and defining it's properties and then adding it to the widget helped as much as setUpdatesEnabled( false ).

    Together they make it super fast. I realize it must have had to redraw the table every time I made a change which would be = table size * 4.

    If I ever get to upgrade addChildren will give me an even greater boost in speed.

    Thanks to both of you.

  9. #7
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Too slow adding & defining items to QTreeWidget

    Quick follow up about the QList<QTreeWidgetItem*>. Will deleting the main node delete the items inside for me? Or should i go through the list and delete each one and then delete the main node?

  10. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Too slow adding & defining items to QTreeWidget

    Quote Originally Posted by jnk5y
    Quick follow up about the QList<QTreeWidgetItem*>. Will deleting the main node delete the items inside for me? Or should i go through the list and delete each one and then delete the main node?
    Yes, all children get automatically deleted when the parent item is deleted. And all the items in the whole tree get automatically deleted when the tree widget is deleted. So not much cleanup actions are required..
    J-P Nurmi

  11. #9
    Join Date
    Jun 2007
    Posts
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Too slow adding & defining items to QTreeWidget

    Hello guys!

    I am also having a problem with QTreeWidget.

    In my application, I sniff packages from a server and print them in the QTreeWidget as I receive them.

    The problem is that after the QTreeWidget has many items, the insertion of new items becomes too slow and the cpu stays at 100%.

    Is there any other kind of widget I can use instead of QTreeWidget to do this? I have to manage lots of items in the view.

    Thanks

  12. #10
    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: Too slow adding & defining items to QTreeWidget

    Quote Originally Posted by Billy Lee Black View Post
    Is there any other kind of widget I can use instead of QTreeWidget to do this? I have to manage lots of items in the view.
    You could try QTableView and a custom model. This way you can optimise item insertion to the maximum, but without any numbers it's hard to say whether it will be fast enough for you (some people reported 10x speed increase with custom model).

  13. #11
    Join Date
    Jun 2007
    Posts
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Too slow adding & defining items to QTreeWidget

    Quote Originally Posted by jacek View Post
    You could try QTableView and a custom model. This way you can optimise item insertion to the maximum, but without any numbers it's hard to say whether it will be fast enough for you (some people reported 10x speed increase with custom model).
    About the numbers, I am talking about 10.000 more items in the item view. It is just like the program Wireshark (Ethereal). So, I will need it very fast, because I am receiveing packages every time to show to the users.

    But thanks for the tip. I will try the QTableView

  14. #12
    Join Date
    Jun 2007
    Posts
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Too slow adding & defining items to QTreeWidget

    Well, I´ve made a custom Model and tried it with QTreeView and QTableView.

    But both are still too slow even though my model is really simple.

    In my application, I have like 8 columns in the view. I have to sniff network packets and print them in the view when I receive them. So, I am adding items to the view a lot of times, and each time I have to fill all the columns for each item added.

    I really don't know anymore what to do. Will I have to implement a custom view too?

  15. #13
    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: Too slow adding & defining items to QTreeWidget

    How do you add items to the model? One cell at a time or whole rows/subtrees?

  16. #14
    Join Date
    Jun 2007
    Posts
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Too slow adding & defining items to QTreeWidget

    Well, I made a class that stores the data for all the columns of a row.

    I set all the texts for the entire row and after that I add the row to the model.

  17. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Too slow adding & defining items to QTreeWidget

    Can we see the code?

  18. #16
    Join Date
    Jun 2007
    Posts
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Too slow adding & defining items to QTreeWidget

    This is my method for adding one item( a row ) to the tableview:

    Qt Code:
    1. void AkViewItemModel::addItem(AkViewWidgetItem *newItem)
    2. {
    3. if(!newItem)
    4. return;
    5. int position = mItemsList.count();
    6. beginInsertRows(QModelIndex(), position, position);
    7. newItem->mViewWidget = mHeaderItem->akViewWidget();
    8. mItemsList.append(newItem);
    9. endInsertRows();
    10. }
    To copy to clipboard, switch view to plain text mode 

    And below is the method to add a list of items:

    Qt Code:
    1. void AkViewItemModel::addItems(QList<AkViewWidgetItem *> items)
    2. {
    3. int beginRow = mItemsList.count();
    4. int numItems = items.count();
    5. beginInsertRows(QModelIndex(), beginRow, beginRow+numItems-1);
    6.  
    7. AkViewWidgetItem *item = NULL;
    8. for(int i = 0; i < numItems; i++)
    9. {
    10. item = items[i];
    11. item->mViewWidget = mHeaderItem->akViewWidget();
    12. mItemsList.append(item);
    13. }
    14. endInsertRows();
    15. }
    To copy to clipboard, switch view to plain text mode 

    When I add a lot of single items, it is really slow! But if I accumulate some items in my application and then add them through a list, it becomes a lot faster and solves my problem.

    Why are these methods beginInsertRows and endInsertRows so slow?

  19. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Too slow adding & defining items to QTreeWidget

    These methods are not slow. But they cause some signals to be emitted and each emition triggers a redraw of all the views displaying the model. So if you add 100 items a second then your application is busy refreshing the views. That's why it's better to accumulate changes and update the model in chunks.

Similar Threads

  1. Removing items properly from qtreewidget item
    By Djony in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2006, 13:20

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.