Disclaimer : I am pretty new to UI and QT.
I have UI that have QListWidget comprising of some numbers (1 to 5), now I am trying to delete the item one by one..
Problem : After deletion of all entries are done, I can still see some entries (specifically 2 & 4).
Code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
static MainWindow w;
w.show();
std::thread([]{
w.display();
}).detach();
a.exec();
return 0;
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
void MainWindow::display()
{
sleep(15);
for(int i = 0; i < 5 ; i++)
{
auto j = ui->listWidget->takeItem(i);
delete j;
}
}
MainWindow::~MainWindow()
{
delete ui;
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QListWidget" name="listWidget">
<property name="geometry">
<rect>
<x>180</x>
<y>20</y>
<width>256</width>
<height>192</height>
</rect>
</property>
<item>
<property name="text">
<string>1</string>
</property>
</item>
<item>
<property name="text">
<string>2</string>
</property>
</item>
<item>
<property name="text">
<string>3</string>
</property>
</item>
<item>
<property name="text">
<string>4</string>
</property>
</item>
<item>
<property name="text">
<string>5</string>
</property>
</item>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>19</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Output:
Questions:
Any help is very much appreciated.
The reason is not really related to Qt but has to do with arrays and loops in general.
Say, initially, I start with a list:
0. Apple
1. Orange
2. Jackfruit
I remove the item at index 0:
0. Orange
1. Jackfruit
Now in the loop, we do i++
, and then remove the item at index 1.
0. Orange
Notice that we completely skipped Orange
because it moved down as we incremented our index.
If you want to delete everything, you can use QListWidget::clear()
instead.
As for memory "corruption", the delete j
is ok, since you need to manage the pointer yourself after calling takeItem
.
On a side note, I wouldn't mix std::thread
with Qt (see comments here).