Clearing QList fails



  • Hi,
    I'm using a loop to clear and simultaneously delete a QList's contents (QTextEdits):
    @QMessageBox::information(0, "counter", "count before: "+QString::number(list.count()), QMessageBox::Ok);
    for(int i = 0; i < list.count(); i++)
    {
    delete list.takeLast();
    }
    QMessageBox::information(0, "counter", "count after: "+QString::number(list.count()), QMessageBox::Ok);@
    Output:
    [quote]count before: 6

    count after: 3[/quote]Can someone tell me why the loop does not remove all it's contents??


  • Moderators

    You can use qDeleteAll() instead. Simple and fast.

    The reason why this code does not work is the following: you are taking last element out of the list, then iterate i, then take another, iterate, and so on until i < list count. So the result is that i is increasing while list.count() is decreasing. This will never clear the whole list. If you want to do it manually, you need to do something like this:
    @
    while (!list.isEmpty()) {
    delete list.takeLast();
    }
    @



  • [quote]The reason why this code does not work is the following: you are taking last element out of the list, then iterate i, then take another, iterate, and so on until i < list count. So the result is that i is increasing while list.count() is decreasing. [/quote]Hehe yeah that was it, I detected the mistake just a few seconds ago by watching the loop about 5min :-D

    [quote]You can use qDeleteAll() instead. Simple and fast.[/quote]Hui, didn't know this functionallity yet. Great thing! I did not realize QtAlgorithms so far, very good, exactly what I need.

    Thank's for your fast support! :)


  • Moderators

    Sure, you are welcome :-) Happy coding!



  • EDIT:
    One more question: All those QTextEdits have been added to a layout before. I think I also have to remove them from the layout, right? The problem is, QVBoxLayout does not support a clear() – or removeAll() function, but I can’t call removeWidget(list.last()) anymore, when I deleted them all with one command (qDeleteAll())… How would you manage this?


  • Moderators

    QObjects delete themselves automatically, you do not need to do anything.

    If your use case is special and you really need to delete a widget/ layout yourself, just call delete or deleteLater() on it: it will automatically delete all children QObjects.

    So, most probably, all you need to do here is to run
    @
    myLayout->deleteLater();
    @

    This will delete the layout and all the items inside it on the next event loop sweep.



  • [quote]QObjects delete themselves automatically, you do not need to do anything[/quote]Ah, ok I thought this only happens when a parent-child relationship exists, so adding a QObject to a QLayout via "addWidget()" sets the Widget to child of QLayout? I always thought, the parent is still the window that contains the layout and setting the layout via window->setLayout() will make all QObjects in QLayout childs of the window?! Don't know, maybe I'm wrong and they are all childs of the layout itsself, so QLayout will recognize when it's childs are deleted...


  • Moderators

    Please check, I am not 100% sure myself right now.



  • Qt's documentation to QLayout::addItem():
    [quote]Note: The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it.[/quote]So yes, you were right :-)

    Thank's again.


Log in to reply
 

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