Results 1 to 4 of 4

Thread: Alternative iteration over a QList<QTreeWidgetItem*>

  1. #1
    Join Date
    May 2010
    Location
    Rousse, Bulgaria
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Alternative iteration over a QList<QTreeWidgetItem*>

    Greetings.
    I have a MySQL table with the following structure:
    Qt Code:
    1. id | name | parent_node
    To copy to clipboard, switch view to plain text mode 
    `id` is int primary key, auto increment enabled.
    `name` is a text string
    `parent_node` is an int and holds the id of the previous node.

    I query the database and populate a QList<QTreeWidgetItem*> item_list;
    Qt Code:
    1. while (query->next())
    2. {
    3. tree->setText(0, query->value(1).toString());
    4. tree->setText(1, query->value(0).toString());
    5. tree->setText(2, query->value(2).toString());
    6. item_list.append(tree);
    7. }
    To copy to clipboard, switch view to plain text mode 

    All is well, but I wonder if my iteration over the list and constructing the tree is optimal. The table contains 10 rows, but in the future it's gonna hold at least 1000 rows and I wonder if the program is going to behave nice, since the tree is constructed on startup (in the main class' constructor).

    Here's my iterations:
    Qt Code:
    1. for (int j=0;j<item_list.count();j++)
    2. {
    3. QTreeWidgetItem* current = item_list[j];
    4. for (int i=0; i<item_list.count();i++)
    5. {
    6. qDebug() << "Current item: " << current->text(0);
    7. if (current->text(2).toInt() == item_list.at(i)->text(1).toInt())
    8. {
    9. item_list.at(i)->addChild(current);
    10. }
    11. else if (current->text(2).toInt()==0)
    12. {
    13. ui->treeWidget->addTopLevelItem(item_list.first());
    14. }
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 
    Thanks for your help!

  2. #2
    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: Alternative iteration over a QList<QTreeWidgetItem*>

    It's definitely not optimal. Put all your items that are going to be child items into a hash keyed by the number you are using for comparison (text(1).toInt()), put the top-level items immediately into the tree and then iterate over all the items once, attaching them to their proper parent extracted from the hash. It will make your algorithm O(n*lgn) instead of O(n*n).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Axtroz (23rd June 2012)

  4. #3
    Join Date
    May 2010
    Location
    Rousse, Bulgaria
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Alternative iteration over a QList<QTreeWidgetItem*>

    Wow, fast and accurate! Thank you very, very much for the quick response!

  5. #4
    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: Alternative iteration over a QList<QTreeWidgetItem*>

    You can make it even faster (O(n)) if you change your original query to return rows in proper order (item, then all its children (with their children), then next top item, all its children, etc.).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 26th September 2011, 08:20
  2. Replies: 4
    Last Post: 20th August 2010, 13:54
  3. How to modify qmap without iteration
    By sanjarbek in forum Newbie
    Replies: 9
    Last Post: 13th April 2010, 10:07
  4. Iteration problem
    By Pharell in forum General Programming
    Replies: 1
    Last Post: 1st October 2007, 10:56
  5. QList and QTreeWidgetItem??
    By darpan in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2006, 14:47

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.