Results 1 to 7 of 7

Thread: How to iterate through QListWidget items

  1. #1
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question How to iterate through QListWidget items

    I need to loop through QListWidget items to save each one of them in a .txt file

    i have no problems with file manipulation all that i need is a way to loop through the list items.

  2. #2
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: How to iterate through QListWidget items

    Try this.....

    Qt Code:
    1. for(int row = 0; row < listWidget->count(); row++)
    2. {
    3. QListWidgetItem *item = listWidget->item(row);
    4. // process item
    5. .......
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    zero-n (26th July 2010)

  4. #3
    Join Date
    Jul 2010
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to iterate through QListWidget items

    Thanks that works for me

  5. #4
    Join Date
    Jan 2012
    Location
    St. Petersburg, Russia
    Posts
    14
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: How to iterate through QListWidget items

    Qt Code:
    1. for(int row = 0; row < listWidget->count(); row++)
    2. {
    3. QListWidgetItem *item = listWidget->item(row);
    4. // process item
    5. .......
    6. }
    To copy to clipboard, switch view to plain text mode 

    AFAIK It will not work if some items were already deleted.


    Added after 14 minutes:


    Can't find better solution than this:

    Qt Code:
    1. foreach(QListWidgetItem* listItem, findItems("*", Qt::MatchWildcard))
    2. {
    3. ...
    4. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by monst; 12th January 2012 at 08:04.

  6. #5
    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: How to iterate through QListWidget items

    Why would anyone want to loop over deleted items? If they are deleted you can't access them because they don't exist anymore. Your code will not return any "deleted items" either because ultimately findItems() calls QAbstractItemModel::match() which in turn calls QAbstractItemModel::index() which returns info on valid aka existing aka not-deleted items.
    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.


  7. #6
    Join Date
    Jan 2012
    Location
    St. Petersburg, Russia
    Posts
    14
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: How to iterate through QListWidget items

    I'm sorry, you've gotten me wrong. I wanted to say that one must be careful if some items are deleted or added in this loop.
    For example, if you want to delete all items, it will be mistake to do it this way:

    Qt Code:
    1. for(int row = 0; row < listWidget->count(); row++)
    2. {
    3. delete listWidget->takeItem(row);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Even this will be a mistake:

    Qt Code:
    1. int c = listWidget->count();
    2. for(int row = 0; row < c; row++)
    3. {
    4. delete listWidget->takeItem(row);
    5. }
    To copy to clipboard, switch view to plain text mode 

    But this will work:

    Qt Code:
    1. foreach(QListWidgetItem* listItem, findItems("*", Qt::MatchWildcard))
    2. {
    3. delete listItem;
    4. }
    To copy to clipboard, switch view to plain text mode 

    You can run this code:

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QListWidget>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8.  
    9. QListWidget* listWidget = new QListWidget();
    10.  
    11. QListWidgetItem* firstItem = new QListWidgetItem("first");
    12. QListWidgetItem* secondItem = new QListWidgetItem("second");
    13. QListWidgetItem* thirdItem = new QListWidgetItem("third");
    14. QListWidgetItem* fourthItem = new QListWidgetItem("fourth");
    15. QListWidgetItem* fifthItem = new QListWidgetItem("fifth");
    16.  
    17. listWidget->addItem(firstItem);
    18. listWidget->addItem(secondItem);
    19. listWidget->addItem(thirdItem);
    20. listWidget->addItem(fourthItem);
    21. listWidget->addItem(fifthItem);
    22.  
    23. qDebug() << "count before deleting is " << listWidget->count();
    24.  
    25. int c = listWidget->count();
    26. for(int row = 0; row < c; row++)
    27. {
    28. delete listWidget->takeItem(row);
    29. qDebug() << "count after iteration" << row << "is " << listWidget->count();
    30. }
    31.  
    32. qDebug() << "count after deleting is " << listWidget->count();
    33.  
    34. qDebug() << "expected nothing...";
    35. for(int row = 0; row < listWidget->count(); row++)
    36. {
    37. qDebug() << listWidget->item(row)->text();
    38. }
    39.  
    40. return 0;
    41. }
    To copy to clipboard, switch view to plain text mode 

    And you will get a following output:

    Qt Code:
    1. count before deleting is 5
    2. count after iteration 0 is 4
    3. count after iteration 1 is 3
    4. count after iteration 2 is 2
    5. count after iteration 3 is 2
    6. count after iteration 4 is 2
    7. count after deleting is 2
    8. expected nothing...
    9. "second"
    10. "fourth"
    To copy to clipboard, switch view to plain text mode 

  8. #7
    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: How to iterate through QListWidget items

    Well, that's kind of obvious (we're discussing a similar issue in a different thread) but you can do this:
    Qt Code:
    1. int c = listWidget->count();
    2. for(int row = 0; row < c; row++)
    3. {
    4. delete listWidget->takeItem(0);
    5. }
    To copy to clipboard, switch view to plain text mode 
    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.


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

    monst (13th January 2012)

Similar Threads

  1. Move items up and down in QListWidget
    By araglin in forum Newbie
    Replies: 7
    Last Post: 31st August 2016, 11:05
  2. Iterate the QListWidget
    By vishal.chauhan in forum Newbie
    Replies: 1
    Last Post: 26th February 2007, 07:51
  3. Get Visible Items from the QListWidget
    By srj in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2006, 20:13
  4. Iterate and get Checked QTable items??
    By darpan in forum Newbie
    Replies: 2
    Last Post: 10th May 2006, 18:27
  5. Getting all items of a QListWidget
    By Codepoet in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2006, 22:52

Tags for this Thread

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.