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

Cannot delete items of a QVBoxLayout



  • Hi!
    this is in the constructor:

    myGroupBox->setLayout(myBoxLayout);
    myBoxLayout->setAlignment(Qt::AlingTop);
    

    and then I call this:

    if(myBoxLayout->takeAt(0) != NULL){
        QLayoutItem *item;
        while((item = myBoxLayout->takeQt(0)) != NULL){
            delete item->widget();
            delete item;
        }
    }
    

    to remove all widgets from the layout.
    The program just crashes. What did I miss?
    This code came from this thread, but I can't find a answer for my problem there:
    https://stackoverflow.com/questions/4272196/qt-remove-all-widgets-from-layout
    Thanks for answers



  • Ahh about 1300 lines, unfortunately not small.
    It works now... aaand yes, I think the deleting of the builddirectory worked.
    What happened:
    I deleted the build directory and then the program crashed at an other point before the setting up of the layout. I found a thing that I would not call a mistake, but it surely is in some way, because of my endless function calling, the function, where I set up the layouts got called twice. I don't really know, why this is a mistake, but after I fixed it, it worked and then the program was able to say that it is a nullpointer. So I changed it back to new QVBoxLayout and everything works.
    The weird thing is, that I really don't know why it was a problem to setting up the layout as a nullpointer twice.
    But, anyway, thanks for your help!


  • Lifetime Qt Champion

    Hi
    Seems ok but there some notes

    
    if(myBoxLayout->takeAt(0) != NULL){ << you take first out first here ( and do not delete it)
        QLayoutItem *item;
        while((item = myBoxLayout->takeQt(0)) != NULL){
            delete item->widget();
            delete item;
        }
    }
    

    IF you single step , what lines does it crash on ?



  • @mrjj
    on the line with the if statement


  • Lifetime Qt Champion

    Hi
    It crashes at
    if(myBoxLayout->takeAt(0) .. ?

    did you forget to new myBoxLayout ?



  • @mrjj
    Nope, this is a class attribute and in the constructor (all in the same class) I do

    myBoxLayout = new QVBoxLayout;
    

    and there is also no error, that works.


  • Lifetime Qt Champion

    @Niagarer
    Cant guess then.

    try
    qDebug() << "items:" << myBoxLayout->count();
    just before the If.



  • @mrjj
    Thanks, I tried and it crashes at exactely this line you gave me.
    I can not do anything with the layout
    Seems to be a nasty one...


  • Lifetime Qt Champion

    @Niagarer
    If it crashes there, something must be wrong with the pointer
    myBoxLayout->count();

    You did show real code, yes ?

    A classic is to define in .h
    and then do
    QVBoxLayout *myBoxLayout = new QVBoxLayout; ( in ctror)
    and make local variable instead.

    set it to null where u define it
    QVBoxLayout *myBoxLayout=nullptr;

    and check if its NON null before using it on the count()



  • @mrjj
    Ok, I tried it.
    I did not make the classic mistake but if I define it with a nullpointer

    myBoxLayout = nullptr;
    

    and then asking if it's nullptr where I try to delete all content, it says, it is not null
    But I only defined it once...
    And if I ask that this way:

    myBoxLayout = nullptr;
    if(myBoxLayout != nullptr){
        qDebug() << "layout is not null";
    }
    

    it says, it is not null...


  • Lifetime Qt Champion

    @Niagarer
    what ??
    myBoxLayout = nullptr;
    if(myBoxLayout != nullptr){
    qDebug() << "layout is not null";
    }

    that code should say nothing. :)
    I assume that
    myBoxLayout = nullptr; <<< in .h yes ?

    Sorry, i have really no idea.
    Its does work that way and i have used takeAt many times.



  • @mrjj
    In the .h file:

        QVBoxLayout *myBoxLayout;
    

    and in the .cpp file:

    myBoxLayout = nullptr;
    if(myBoxLayout == nullptr){
        qDebug() << "layout is null";
    }
    

    and it prints nothing...
    I really don't understand that too.
    I just hope that I am not doing a totally dumb mistake here...


  • Lifetime Qt Champion

    @Niagarer
    Hi, no worries, we all do stupid mistakes sometimes.
    Its a sign of a productive programmer ;)

    first
    QVBoxLayout *myBoxLayout; -> QVBoxLayout *myBoxLayout=nullptr.
    in .h
    then do NOT
    myBoxLayout = nullptr; before the check. ( you kill it )

    also , it must print something ???
    myBoxLayout = nullptr;
    if(myBoxLayout == nullptr){
    qDebug() << "layout is null";
    }
    else something is really, really wrong.

    Can you test this small sample ?
    https://www.dropbox.com/s/tkl1hdik6q0qwl2/myreuselayout.zip?dl=0
    Uses your code to remove buttons from layout.



  • @mrjj
    Now, it does not print any qDebugs() anymore... what the hell!?



  • Close Creator, delete the build directory and start Creator again. Sometime strange things happen.



  • Ok, I did and now I have a mistake at a point in the code before the setting up of the layout.
    I add a Tab of a tabWidget and there it crashes.,.. This was never a problem, this line works sonce weeks, I did not change anything there.
    I deleted the build directory, opened creator again, ran qmake, rebuilt and that's it, no errors, it is just crashing



  • @mrjj
    Yep, this project works. So it has to be my bad.



  • I say "unitialized memory somewhere".


  • Lifetime Qt Champion

    I agree @Wieland , the big question is just Where :)

    @Niagarer
    If its a small project, can you zip and link it here ?



  • Ahh about 1300 lines, unfortunately not small.
    It works now... aaand yes, I think the deleting of the builddirectory worked.
    What happened:
    I deleted the build directory and then the program crashed at an other point before the setting up of the layout. I found a thing that I would not call a mistake, but it surely is in some way, because of my endless function calling, the function, where I set up the layouts got called twice. I don't really know, why this is a mistake, but after I fixed it, it worked and then the program was able to say that it is a nullpointer. So I changed it back to new QVBoxLayout and everything works.
    The weird thing is, that I really don't know why it was a problem to setting up the layout as a nullpointer twice.
    But, anyway, thanks for your help!


  • Lifetime Qt Champion

    Super :)
    Im not sure what calling the function twice could be issue but it did do something it seems.


Log in to reply