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. How to move a widget from a layout to another layout?
QtWS25 Last Chance

How to move a widget from a layout to another layout?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 4.6k Views
  • 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.
  • H Offline
    H Offline
    HelloWorld88
    wrote on last edited by
    #1

    hello all!!

    im new in Qt and C++, I want to move 2 widget from a vertical layout to horizontal layout when a button is clicked. I already tried two days in a row and dont know how to solve that problem... (I already designed the UI and a sliding menu writing in C++)

    example:
    when I click the burger button I want those two button below the burger button move to the same layout as the burger button.

    1) Main Page.png

    after clicking

    6) Hamburger Open.png

    Thanks you!!

    jsulmJ 1 Reply Last reply
    0
    • H HelloWorld88

      hello all!!

      im new in Qt and C++, I want to move 2 widget from a vertical layout to horizontal layout when a button is clicked. I already tried two days in a row and dont know how to solve that problem... (I already designed the UI and a sliding menu writing in C++)

      example:
      when I click the burger button I want those two button below the burger button move to the same layout as the burger button.

      1) Main Page.png

      after clicking

      6) Hamburger Open.png

      Thanks you!!

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @HelloWorld88 Find out the index of the widget you want to move in the current layout using https://doc.qt.io/qt-5/qlayout.html#indexOf
      Then remove the widget from the current layout using https://doc.qt.io/qt-5/qlayout.html#takeAt
      Then add the widget to other layout using https://doc.qt.io/qt-5/qlayout.html#addWidget

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

      JonBJ 1 Reply Last reply
      5
      • jsulmJ jsulm

        @HelloWorld88 Find out the index of the widget you want to move in the current layout using https://doc.qt.io/qt-5/qlayout.html#indexOf
        Then remove the widget from the current layout using https://doc.qt.io/qt-5/qlayout.html#takeAt
        Then add the widget to other layout using https://doc.qt.io/qt-5/qlayout.html#addWidget

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @jsulm
        I think you can skip indexOf() + takeAt() by using https://doc.qt.io/qt-5/qlayout.html#removeWidget instead? (Though you may get no warning if the widget isn't found, where indexOf() would have let you know.)

        jsulmJ H 2 Replies Last reply
        5
        • JonBJ JonB

          @jsulm
          I think you can skip indexOf() + takeAt() by using https://doc.qt.io/qt-5/qlayout.html#removeWidget instead? (Though you may get no warning if the widget isn't found, where indexOf() would have let you know.)

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

          @JonB Sure, you're right

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

          1 Reply Last reply
          1
          • JonBJ JonB

            @jsulm
            I think you can skip indexOf() + takeAt() by using https://doc.qt.io/qt-5/qlayout.html#removeWidget instead? (Though you may get no warning if the widget isn't found, where indexOf() would have let you know.)

            H Offline
            H Offline
            HelloWorld88
            wrote on last edited by HelloWorld88
            #5

            @JonB Im using removeWidget(), but now how I can remove the Stretch() that I add previously in the Layout?
            @jsulm I will try now with you recommendation.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Where is the stretch located ? Based on that you can use the takeAt method.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • S Offline
                S Offline
                SimonSchroeder
                wrote on last edited by
                #7

                @JonB @jsulm You don't need takeAt nor removeWidget. Every widget can only be a child of one parent. If you use addWidget to add the widget to a different layout it will be reparented and thus be removed from the previous layout.

                @HelloWorld88 Removing the stretch is a little bit more complicated. How did you add it? Did you create your layout using ui-files or did you assemble your widgets in pure C++ code? Did you use addStretch() to add the stretch? Maybe @SGaist 's method already works.

                JonBJ 1 Reply Last reply
                3
                • S SimonSchroeder

                  @JonB @jsulm You don't need takeAt nor removeWidget. Every widget can only be a child of one parent. If you use addWidget to add the widget to a different layout it will be reparented and thus be removed from the previous layout.

                  @HelloWorld88 Removing the stretch is a little bit more complicated. How did you add it? Did you create your layout using ui-files or did you assemble your widgets in pure C++ code? Did you use addStretch() to add the stretch? Maybe @SGaist 's method already works.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @SimonSchroeder
                  Interesting! Seems a little bit dodgy to me --- I'd be fearful of getting a warning that the widget was already added to some other layout and I should take/remove it first! But I respect what you say.

                  Since the OP announced he now wants to move a stretch and not just a widget, I would have thought he will want to use indexOf() to locate the widget and then from that locate the stretch as the previous/next sibling to remove, or similar.

                  1 Reply Last reply
                  1
                  • H Offline
                    H Offline
                    HelloWorld88
                    wrote on last edited by HelloWorld88
                    #9

                    @JonB @jsulm @SGaist @SimonSchroeder thanks you all for you replies I already solve the problem yesterday of the stretch, I used TakeAt method.

                            QLayoutItem *stretch;
                            stretch = mainMenuVLayout->takeAt(3);
                            delete stretch;
                    
                    S 1 Reply Last reply
                    1
                    • H HelloWorld88

                      @JonB @jsulm @SGaist @SimonSchroeder thanks you all for you replies I already solve the problem yesterday of the stretch, I used TakeAt method.

                              QLayoutItem *stretch;
                              stretch = mainMenuVLayout->takeAt(3);
                              delete stretch;
                      
                      S Offline
                      S Offline
                      SimonSchroeder
                      wrote on last edited by
                      #10

                      @HelloWorld88 said in How to move a widget from a layout to another layout?:

                      I already solve the problem yesterday of the stretch

                      Thank you for sharing. Don't forget to mark your answer as correct.

                      1 Reply Last reply
                      0

                      • Login

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