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. Correct way to remove content from layout?
Forum Updated to NodeBB v4.3 + New Features

Correct way to remove content from layout?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 234 Views 3 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    I'm using Qt 5.9.2 on Windows 10 with MSVC2015. I have a function:

    void MyClass::removeContent(QLayout* pLayout) {
        if ( pLayout == nullptr ) {
            return;
        }
        while( QLayoutItem* pItem = pLayout->takeAt(0) ) {
            if ( QWidget* pWidget = pItem->widget() ) {
                pWidget->deleteLater();
            } else if ( QLayout* pChildLayout = pItem->layout() ) {
                removeContent(pChildLayout);
                pChildLayout->deleteLater();
            }
        }
    }
    

    I am seeing:

    QFormLayout::takeAt: Invalid index 0
    

    Is there anything I can do to prevent this message from occurring?

    I've modified the initial if condition to:

        if ( pLayout == nullptr || pLayout->children().size() == 0  ) {
    

    [Edit], changed again to:

        if ( pLayout == nullptr || pLayout->count() == 0  ) {
    

    Still seeing the same thing.

    Kind Regards,
    Sy

    raven-worxR jsulmJ 2 Replies Last reply
    0
    • SPlattenS SPlatten

      @raven-worx thank you, just tried that, same result.

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #5

      @SPlatten what about

      while(  pLayout->count() ) {
              QLayoutItem* pItem = pLayout->takeAt(0);
              if ( QWidget* pWidget = pItem->widget() ) {
                  pWidget->deleteLater();
              } else if ( QLayout* pChildLayout = pItem->layout() ) {
                  removeContent(pChildLayout);
                  pChildLayout->deleteLater();
              }
          }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      SPlattenS 1 Reply Last reply
      3
      • SPlattenS SPlatten

        I'm using Qt 5.9.2 on Windows 10 with MSVC2015. I have a function:

        void MyClass::removeContent(QLayout* pLayout) {
            if ( pLayout == nullptr ) {
                return;
            }
            while( QLayoutItem* pItem = pLayout->takeAt(0) ) {
                if ( QWidget* pWidget = pItem->widget() ) {
                    pWidget->deleteLater();
                } else if ( QLayout* pChildLayout = pItem->layout() ) {
                    removeContent(pChildLayout);
                    pChildLayout->deleteLater();
                }
            }
        }
        

        I am seeing:

        QFormLayout::takeAt: Invalid index 0
        

        Is there anything I can do to prevent this message from occurring?

        I've modified the initial if condition to:

            if ( pLayout == nullptr || pLayout->children().size() == 0  ) {
        

        [Edit], changed again to:

            if ( pLayout == nullptr || pLayout->count() == 0  ) {
        

        Still seeing the same thing.

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #2

        @SPlatten
        pLayout->children().size() should rather be pLayout->count()

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        SPlattenS 1 Reply Last reply
        3
        • raven-worxR raven-worx

          @SPlatten
          pLayout->children().size() should rather be pLayout->count()

          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #3

          @raven-worx thank you, just tried that, same result.

          Kind Regards,
          Sy

          J.HilkJ 1 Reply Last reply
          0
          • SPlattenS SPlatten

            I'm using Qt 5.9.2 on Windows 10 with MSVC2015. I have a function:

            void MyClass::removeContent(QLayout* pLayout) {
                if ( pLayout == nullptr ) {
                    return;
                }
                while( QLayoutItem* pItem = pLayout->takeAt(0) ) {
                    if ( QWidget* pWidget = pItem->widget() ) {
                        pWidget->deleteLater();
                    } else if ( QLayout* pChildLayout = pItem->layout() ) {
                        removeContent(pChildLayout);
                        pChildLayout->deleteLater();
                    }
                }
            }
            

            I am seeing:

            QFormLayout::takeAt: Invalid index 0
            

            Is there anything I can do to prevent this message from occurring?

            I've modified the initial if condition to:

                if ( pLayout == nullptr || pLayout->children().size() == 0  ) {
            

            [Edit], changed again to:

                if ( pLayout == nullptr || pLayout->count() == 0  ) {
            

            Still seeing the same thing.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @SPlatten said in Correct way to remove content from layout?:

            QFormLayout::takeAt: Invalid index 0

            Well, on last iteration you will get this warning as there is nothing to take from the layout anymore. Replace your while() loop with a for().

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • SPlattenS SPlatten

              @raven-worx thank you, just tried that, same result.

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by
              #5

              @SPlatten what about

              while(  pLayout->count() ) {
                      QLayoutItem* pItem = pLayout->takeAt(0);
                      if ( QWidget* pWidget = pItem->widget() ) {
                          pWidget->deleteLater();
                      } else if ( QLayout* pChildLayout = pItem->layout() ) {
                          removeContent(pChildLayout);
                          pChildLayout->deleteLater();
                      }
                  }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

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

                Hi

                I was wondering if

                while( pLayout->count && QLayoutItem* pItem = pLayout->takeAt(0) )

                would do it ?

                ps. I like @J-Hilk version better as we don't have to rely on lazy evaluation from the compiler

                1 Reply Last reply
                1
                • J.HilkJ J.Hilk

                  @SPlatten what about

                  while(  pLayout->count() ) {
                          QLayoutItem* pItem = pLayout->takeAt(0);
                          if ( QWidget* pWidget = pItem->widget() ) {
                              pWidget->deleteLater();
                          } else if ( QLayout* pChildLayout = pItem->layout() ) {
                              removeContent(pChildLayout);
                              pChildLayout->deleteLater();
                          }
                      }
                  
                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #7

                  @J-Hilk , perfect, thank you!

                  Kind Regards,
                  Sy

                  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