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:
- File : main.cpp
Here I have created one async thread that will call MainWindows::display function.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
static MainWindow w;
w.show();
std::thread([]{
w.display();
}).detach();
a.exec();
return 0;
}
- File : mainwindow.cpp
Here display function is deleting the items.
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;
}
- File : mainwindow.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:
- Why this is happening? am I doing something wrong?
- If this leads to undeletion/corruption is there any other way to achieve it?
Any help is very much appreciated.