I am trying to add two items into my QListWidget dynamically. However, the following codes only allow me to add only the last item into the list. strList.size() contains 4 items. Assuming name contains "ABC 1" and "ABC 2".
Is my loop incorrect? Or is my method of adding items into the listWidget wrong?
.h:
public:
public:
QListWidgetItem *item[2];
To copy to clipboard, switch view to plain text mode
.cpp:
int num = 0;
for(int i = 0; i < strList.size(); i++)
{
if(strList[i] == "ABC")
{
...
item[num]->setText(name);
ui.listWidget->insertItem(num, item[num]);
num += 1;
}
}
int num = 0;
for(int i = 0; i < strList.size(); i++)
{
if(strList[i] == "ABC")
{
...
item[num] = new QListWidgetItem();
item[num]->setText(name);
ui.listWidget->insertItem(num, item[num]);
num += 1;
}
}
To copy to clipboard, switch view to plain text mode
Output (listWidget):
Expected output (listWidget):
Bookmarks