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 24 Jun 2020, 13:07 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!!

    J 1 Reply Last reply 24 Jun 2020, 13:20
    0
    • H HelloWorld88
      24 Jun 2020, 13:07

      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!!

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 24 Jun 2020, 13:20 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

      J 1 Reply Last reply 24 Jun 2020, 13:31
      5
      • J jsulm
        24 Jun 2020, 13:20

        @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

        J Offline
        J Offline
        JonB
        wrote on 24 Jun 2020, 13:31 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.)

        J H 2 Replies Last reply 24 Jun 2020, 13:34
        5
        • J JonB
          24 Jun 2020, 13:31

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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 24 Jun 2020, 13:34 last edited by
          #4

          @JonB Sure, you're right

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

          1 Reply Last reply
          1
          • J JonB
            24 Jun 2020, 13:31

            @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 24 Jun 2020, 14:29 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
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 24 Jun 2020, 19:03 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 25 Jun 2020, 07:13 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.

                J 1 Reply Last reply 25 Jun 2020, 07:42
                3
                • S SimonSchroeder
                  25 Jun 2020, 07:13

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

                  J Offline
                  J Offline
                  JonB
                  wrote on 25 Jun 2020, 07:42 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 25 Jun 2020, 07:55 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 26 Jun 2020, 07:30
                    1
                    • H HelloWorld88
                      25 Jun 2020, 07:55

                      @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 26 Jun 2020, 07:30 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

                      10/10

                      26 Jun 2020, 07:30

                      • Login

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