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);
}
for(int row = 0; row < listWidget->count(); row++)
{
delete listWidget->takeItem(row);
}
To copy to clipboard, switch view to plain text mode
Even this will be a mistake:
int c = listWidget->count();
for(int row = 0; row < c; row++)
{
delete listWidget->takeItem(row);
}
int c = listWidget->count();
for(int row = 0; row < c; row++)
{
delete listWidget->takeItem(row);
}
To copy to clipboard, switch view to plain text mode
But this will work:
{
delete listItem;
}
foreach(QListWidgetItem* listItem, findItems("*", Qt::MatchWildcard))
{
delete listItem;
}
To copy to clipboard, switch view to plain text mode
You can run this code:
#include <QtGui/QApplication>
#include <QListWidget>
#include <QDebug>
int main(int argc, char *argv[])
{
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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
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"
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"
To copy to clipboard, switch view to plain text mode
Bookmarks