Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QListWidgetItem removal does not work for the last element



  • This is my code:

    void MainWindow::on_pushButtonDELETE_clicked()
    {
        QList<QListWidgetItem*> items = ui->listWidget->selectedItems();
        foreach(QListWidgetItem * item, items)
        {
            delete ui->listWidget->takeItem(ui->listWidget->row(item));
        }
    }
    

    There is no NULL pointer passed to the ui->listWidget->takeItem.

    It removes item on the list but when there is one item on the list, removing causes"Invalid parameter passed to C runtime function" and program crashes.
    Where is the problem of that?


  • Lifetime Qt Champion

    Hi
    Tried the same code in Qt5.13 with a clean project and a ListWidget and nothing
    bad happened.
    Just deleted the single line. I tried adding many and delete some of them, then the last.
    All cases worked.


  • Lifetime Qt Champion

    Hi,

    What OS are you running ?



  • Win7 Qt 5.9.2


  • Lifetime Qt Champion

    Hi
    Did you try the same code in clean project to validate its actually that?



  • @BartoszT
    Although your code is actually supposed to be probably OK, especially since @mrjj says it worked fine for him, there occurs to me small possibility. You are deleting all items on list, just maybe the foreach has an issue moving off the last one it has deleted because there are no remaining items. As I say I doubt it, but why not try deleting by, say, index instead? Along the lines of:

    int num = items.count();
    for (int i = 0; i < num; i++)
        delete items.takeAt(0);   //Always delete element 0
    

    Any difference in behaviour?

    Separately, make sure your own code does not have, say, a slot on list item removal which relies on there still being one selected item and crashes if not? If you can, running it inside debugger should show you where this error/crash is emanating from.



  • Thanks for the advice,
    I also use the on_listWidget_currentRowChanged slot, after deleting the last item in the list, the current currentRow number assumes the value -1 and it causes a problem. The list is linked to the pointers container and the data is fetched from the object associated with the pointer.


  • Lifetime Qt Champion

    @BartoszT said in QListWidgetItem removal does not work for the last element:

    currentRowChanged

    QItemSelectionModel::currentRowChanged() will emit an invalid QModelIndex when there is nothing selected so -1 is correct here.
    Please show us a minimal compilable example since noone except you can reproduce a problem here.



  • For this additional one comment, program crashes.
    Without comment, program works.

    0_1563371761908_prog.png



  • @BartoszT
    Yes, well (with the check line commented out) going TachoList.at(-1) is going to be the source of your ""Invalid parameter passed to C runtime function" and program crashes" problem!

    @Christian-Ehrlicher has said currentRow can indeed be -1, so it's your responsibility to deal with that in whatever code you add.

    Which is why I originally wondered whether you had code elsewhere relying on there being at least one selected item :)


Log in to reply