[Solved]Segfault when deleting last item from QListWidget
-
My app segfaults whenever the last item from the QListWidget is deleted. The list widget contains a list of QStrings, which is inserted into it by:
@for( int i=0; i < fileList->size(); i++ )
ui->fileListWidget->addItem(fileList->at(i) );@The app crashes on executing ui->ListWidgetObj->clear() only if an item in the list is selected (by clicking on it).
qDeleteAll( ui->fileListWidget->selectedItems() ); also causes it to segfault if it contains the last item
I also tried:
@QList<QListWidgetItem*> items = ui->fileListWidget->selectedItems();
QListWidgetItem *i ;
foreach(i,items) {
delete ui->fileListWidget->takeItem(ui->fileListWidget->row(i));@gdb gives this output
@Program received signal SIGSEGV, Segmentation fault.
0x000000000040757f in QListWidgetItem::text (this=0x0) at ../QtSDK/Desktop/Qt/4.8.1/gcc/include/QtGui/qlistwidget.h:90
90 { return data(Qt::DisplayRole).toString(); }@Here is the backtrace http://pastebin.ca/2162862
-
Maybe you have to use the removeItemWidget(..) method.
Or you could change delete to deleteLater
@
QList<QListWidgetItem*> items = ui->fileListWidget->selectedItems();
for( int i = items.size()-1; i >= 0; i--)
{
ui->fileListWidget->removeItemWidget(items.at(i));
}
// or alternatively
QListWidgetItem *i ;
foreach(i,items) {
ui->fileListWidget->takeItem(ui->fileListWidget->row(i))->deleteLater();}
@EDIT: corrected the errors in the code examples.
-
both of them didnt work, shouldn't it be from items.size() -1
@ for( int i = items.size() - 1 ; i >= 0; i--) {
ui->fileListWidget->removeItemWidget(items.at(i)); }@
and according to the manual prototype of deleteLater() is
@void QObject::deleteLater () [slot]@ -
Thanks for correcting the mistakes. Sorry the suggestions didn't solve your problems.
Maybe "unselecting" the items before you remove them can solve your problem ?Another possible approach is to get the selected indexes and remove them via the model.
@
QModelIndexList selectedItems = yourListWidget->selectionModel()->selectedIndexes();
for(int i = 0; i < selectedItems.count(); i++)
{
yourListWidget->model()->removeRow(selectedItems.at(i).row(), selectedItems.at(i));
}
@ -
i fixed the issue. I had a onCurrentItemChanged() slot which did not check for null pointer