Remove the only item from a QListWidget
-
Hello,
I am trying to have a button remove the currently selected item from a QListWidget, using
ui->listWidget->takeItem(ui->listWidget->currentRow());
It works, except for when I attempt to delete the ONLY item in the list: in that case, the program crashes. Is there anything I am missing about the takeItem function?
Thank you in advance
-
Hello,
I am trying to have a button remove the currently selected item from a QListWidget, using
ui->listWidget->takeItem(ui->listWidget->currentRow());
It works, except for when I attempt to delete the ONLY item in the list: in that case, the program crashes. Is there anything I am missing about the takeItem function?
Thank you in advance
@p_Each
You sure it "crashes" when there is one item rather than no items?qDebug() << ui->listWidget->currentRow()
.Otherwise I guess (though surprised) maybe it doesn't like having no item as the current row. I don't know if
setCurrentRow(-1)
is allowed. Or, are you doing this inside somewhere which expects there to be a current row/at least one list item? -
I have tried to have the empty list, add the item (I see the element added in the listwidget), and delete it right after that, this is why I assumed the count is 1 when I try to delete it.
The output of qDebug says currentRow is 0 before I try to delete, which makes sense to me, so I guess I'll look for the issue somewhere else -
I have tried to have the empty list, add the item (I see the element added in the listwidget), and delete it right after that, this is why I assumed the count is 1 when I try to delete it.
The output of qDebug says currentRow is 0 before I try to delete, which makes sense to me, so I guess I'll look for the issue somewhere elsein that case, the program crashes
You should run your program in the debugger. When it crashes you should be shown a stack trace, identifying exactly where it is and where it was called from. Maybe it's from a different place which is executed subsequently and expects a non-empty list?
-
Ok, that was something really stupid on my part, but your comment put me in the right direction.
I have a QTextEdit in which I show some information about the currently selected list item. When I delete the only item from the list, the currentRow becomes negative and the content of the QTextEdit can't be updated correctly.
Thanks