Home > Software design >  Deleting items from QT's QListWidget leading to undeletion/corruption of other entries
Deleting items from QT's QListWidget leading to undeletion/corruption of other entries

Time:11-23

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:

  1. 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;
}
  1. 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;
}
  1. File : mainwindow.UI
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget  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  name="centralwidget">
   <widget  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  name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>19</height>
    </rect>
   </property>
  </widget>
  <widget  name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

Output:

enter image description here

Questions:

  1. Why this is happening? am I doing something wrong?
  2. If this leads to undeletion/corruption is there any other way to achieve it?

Any help is very much appreciated.

CodePudding user response:

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).

  • Related