Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot delete items of a QVBoxLayout

Cannot delete items of a QVBoxLayout

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 3 Posters 5.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on last edited by Niagarer
    #1

    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

    1 Reply Last reply
    0
    • mrjjM mrjj

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

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

      NiagarerN Offline
      NiagarerN Offline
      Niagarer
      wrote on last edited by Niagarer
      #19

      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!

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        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 ?

        NiagarerN 1 Reply Last reply
        0
        • mrjjM mrjj

          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 ?

          NiagarerN Offline
          NiagarerN Offline
          Niagarer
          wrote on last edited by
          #3

          @mrjj
          on the line with the if statement

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #4

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

            did you forget to new myBoxLayout ?

            NiagarerN 1 Reply Last reply
            1
            • mrjjM mrjj

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

              did you forget to new myBoxLayout ?

              NiagarerN Offline
              NiagarerN Offline
              Niagarer
              wrote on last edited by Niagarer
              #5

              @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.

              mrjjM 1 Reply Last reply
              0
              • NiagarerN Niagarer

                @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.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @Niagarer
                Cant guess then.

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

                NiagarerN 1 Reply Last reply
                1
                • mrjjM mrjj

                  @Niagarer
                  Cant guess then.

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

                  NiagarerN Offline
                  NiagarerN Offline
                  Niagarer
                  wrote on last edited by Niagarer
                  #7

                  @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...

                  mrjjM 1 Reply Last reply
                  0
                  • NiagarerN Niagarer

                    @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...

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #8

                    @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()

                    NiagarerN 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @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()

                      NiagarerN Offline
                      NiagarerN Offline
                      Niagarer
                      wrote on last edited by Niagarer
                      #9

                      @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...

                      mrjjM 1 Reply Last reply
                      0
                      • NiagarerN Niagarer

                        @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...

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @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.

                        NiagarerN 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @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.

                          NiagarerN Offline
                          NiagarerN Offline
                          Niagarer
                          wrote on last edited by Niagarer
                          #11

                          @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...

                          mrjjM 1 Reply Last reply
                          0
                          • NiagarerN Niagarer

                            @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...

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @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.

                            NiagarerN 2 Replies Last reply
                            1
                            • mrjjM mrjj

                              @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.

                              NiagarerN Offline
                              NiagarerN Offline
                              Niagarer
                              wrote on last edited by
                              #13

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

                              1 Reply Last reply
                              0
                              • ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by
                                #14

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

                                1 Reply Last reply
                                0
                                • NiagarerN Offline
                                  NiagarerN Offline
                                  Niagarer
                                  wrote on last edited by Niagarer
                                  #15

                                  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

                                  1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @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.

                                    NiagarerN Offline
                                    NiagarerN Offline
                                    Niagarer
                                    wrote on last edited by
                                    #16

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

                                    1 Reply Last reply
                                    0
                                    • ? Offline
                                      ? Offline
                                      A Former User
                                      wrote on last edited by
                                      #17

                                      I say "unitialized memory somewhere".

                                      mrjjM 1 Reply Last reply
                                      1
                                      • ? A Former User

                                        I say "unitialized memory somewhere".

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #18

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

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

                                        NiagarerN 1 Reply Last reply
                                        0
                                        • mrjjM mrjj

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

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

                                          NiagarerN Offline
                                          NiagarerN Offline
                                          Niagarer
                                          wrote on last edited by Niagarer
                                          #19

                                          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!

                                          1 Reply Last reply
                                          1
                                          • mrjjM Offline
                                            mrjjM Offline
                                            mrjj
                                            Lifetime Qt Champion
                                            wrote on last edited by
                                            #20

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

                                            1 Reply Last reply
                                            1

                                            • Login

                                            • Login or register to search.
                                            • First post
                                              Last post
                                            0
                                            • Categories
                                            • Recent
                                            • Tags
                                            • Popular
                                            • Users
                                            • Groups
                                            • Search
                                            • Get Qt Extensions
                                            • Unsolved