PDA

View Full Version : How to iterate through QListWidget items



zero-n
24th July 2010, 10:28
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.

saa7_go
24th July 2010, 10:39
Try this.....



for(int row = 0; row < listWidget->count(); row++)
{
QListWidgetItem *item = listWidget->item(row);
// process item
.......
}

zero-n
24th July 2010, 11:57
Thanks that works for me

monst
12th January 2012, 07:54
for(int row = 0; row < listWidget->count(); row++)
{
QListWidgetItem *item = listWidget->item(row);
// process item
.......
}

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

Added after 14 minutes:

Can't find better solution than this:



foreach(QListWidgetItem* listItem, findItems("*", Qt::MatchWildcard))
{
...
}

wysota
12th January 2012, 11:47
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.

monst
13th January 2012, 07:02
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:



for(int row = 0; row < listWidget->count(); row++)
{
delete listWidget->takeItem(row);
}


Even this will be a mistake:



int c = listWidget->count();
for(int row = 0; row < c; row++)
{
delete listWidget->takeItem(row);
}


But this will work:



foreach(QListWidgetItem* listItem, findItems("*", Qt::MatchWildcard))
{
delete listItem;
}


You can run this code:



#include <QtGui/QApplication>
#include <QListWidget>
#include <QDebug>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QListWidget* listWidget = new QListWidget();

QListWidgetItem* firstItem = new QListWidgetItem("first");
QListWidgetItem* secondItem = new QListWidgetItem("second");
QListWidgetItem* thirdItem = new QListWidgetItem("third");
QListWidgetItem* fourthItem = new QListWidgetItem("fourth");
QListWidgetItem* fifthItem = new QListWidgetItem("fifth");

listWidget->addItem(firstItem);
listWidget->addItem(secondItem);
listWidget->addItem(thirdItem);
listWidget->addItem(fourthItem);
listWidget->addItem(fifthItem);

qDebug() << "count before deleting is " << listWidget->count();

int c = listWidget->count();
for(int row = 0; row < c; row++)
{
delete listWidget->takeItem(row);
qDebug() << "count after iteration" << row << "is " << listWidget->count();
}

qDebug() << "count after deleting is " << listWidget->count();

qDebug() << "expected nothing...";
for(int row = 0; row < listWidget->count(); row++)
{
qDebug() << listWidget->item(row)->text();
}

return 0;
}


And you will get a following output:



count before deleting is 5
count after iteration 0 is 4
count after iteration 1 is 3
count after iteration 2 is 2
count after iteration 3 is 2
count after iteration 4 is 2
count after deleting is 2
expected nothing...
"second"
"fourth"

wysota
13th January 2012, 10:09
Well, that's kind of obvious (we're discussing a similar issue in a different thread) but you can do this:

int c = listWidget->count();
for(int row = 0; row < c; row++)
{
delete listWidget->takeItem(0);
}