Crash of QListWidget when renaming item by program



  • Hello,

    I have a QListWidget to which I add items with the following:
    @MyList->addItem(new QListWidgetItem("MyItemName");@

    Then when I run the code, I can click on the different items and my code takes the proper actions. No problem with that!

    The problem arises when, for some reasons that are too long (and useless) to explain I call a function that renames the selected item. This function contains the line:
    @MyList->currentItem()->setText("New name");@

    When this function is called, the item gets properly renamed. But if I select another item and reselect the item that I just renamed, then the code crashes with the error message:
    [quote]ASSERT failure in QVector<T>::operator[]: "index out of range", file /usr/include/qt5/QtCore/qvector.h, line 369[/quote]

    Anything I am doing wrong?



  • Not with the two lines you posted. The problem has to do with the rest of your code.



  • But if I comment the line
    @MyList->currentItem()->setText("New name");@

    then the code doesn't crash when I select another item and reselect the item that I want to change the name.



  • As msue said it is hard to tell what happens with only two lines of code.
    I guess that selected and current items are not the same.
    You can have a "list":http://qt-project.org/doc/qt-5/qlistwidget.html#selectedItems of selected items but a current item is only one.

    Try to check what is the result of MyList->currentItem() before trying to set a text.



  • Thank you, I'll check that!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.