Question

My application crashes, when I try to clear() all items from a QListWidget and at least one item is selected. Calling clearSelection() first causes the program to crash, too (at the clearSelection() call). Removing the items in a while loop, leads also to a crash. The error-message is

ASSERT: "!isEmpty()" in file /usr/include/qt4/QtCore/qlist.h, line 282.

Some example code:

void MainWindow::clearListWidget()
{
    // ui->listWidget->clearSelection(); // --> causes crash
    // ui->listWidget->clear();          // --> also causes crash
    while(ui->listWidget->count()>0)     // --> no crash calling count()
        ui->listWidget->takeItem(0);     // --> crash again
}

As mentioned, the application only crashes, if items are selected. If nothing is selected, then the methods above work as intended. I work with Qt 4.8.4 on Ubuntu.

I would be thankful for any suggestions, how I can solve the problem.

Was it helpful?

Solution

I have found a solution on my own. The problem was seemingly caused by accessing a selected item in a slot-method, that was connected to the signal itemSelectionChanged(). Here I accessed the text of the selected item via

string text = ui->listWidget->selectedItems().first()->text().toStdString();

Afterwards the crash appeared as described in my question by calling e.g. clear(). I guess the selection process is not finished, when itemSelectionChanged() is emitted and the QListWidget gets somehow confused, when the selected items are already accessed at this point of time. After replacing the signal by itemClicked(QListWidgetItem*), the application no longer crashed.

OTHER TIPS

ui->listWidget->blockSignals(true);
ui->listWidget->clear();
ui->listWidget->blockSignals(false);

enter image description here

This worked in my case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top