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 force a widget to fill the empty space of another hidden widget?

How force a widget to fill the empty space of another hidden widget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 5.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.
  • B Offline
    B Offline
    Bamshad
    wrote on 23 Apr 2019, 18:40 last edited by
    #1

    Hi,

    I work on a application where it's GUI looks this:


    | 1 | 2 |

    | 3 | 4 |

    All the 4 widgets are shown (not hidden) at the beginning. It has a menu with view options where the user can hide widget number 2, 3 and 4.

    I want the widget number one stretch and use the empty place of the other hidden widgets. The size policy is expanding for both vertical and horizontal. I have changed the stretch horizontal and vertical to 100 but it did not work. Also, there is no limitation in maximum size of the widgets.

    I should mention that the whole layout is in Grid format.

    I would be thankful if someone help me :)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 Apr 2019, 19:17 last edited by
      #2

      Hi,

      What exact layout are you using ?

      Grids can be achieved using QGridLayout but also a combination of QVBoxLayout and QHBoxLayout.

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

      B 1 Reply Last reply 23 Apr 2019, 19:27
      1
      • S SGaist
        23 Apr 2019, 19:17

        Hi,

        What exact layout are you using ?

        Grids can be achieved using QGridLayout but also a combination of QVBoxLayout and QHBoxLayout.

        B Offline
        B Offline
        Bamshad
        wrote on 23 Apr 2019, 19:27 last edited by
        #3

        @SGaist I used the default grid system of Qt creator for the Main Window. In the centeralWdiget, the layout name is gridLayout. So, I think it is the the QGridLayout.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 23 Apr 2019, 19:41 last edited by
          #4

          In that case you have to change the span of widget 1 when hiding widget 2. This likely means that you have to pull widget one of the grid and put it back in with the appropriate span value.

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

          B 1 Reply Last reply 23 Apr 2019, 19:57
          1
          • S SGaist
            23 Apr 2019, 19:41

            In that case you have to change the span of widget 1 when hiding widget 2. This likely means that you have to pull widget one of the grid and put it back in with the appropriate span value.

            B Offline
            B Offline
            Bamshad
            wrote on 23 Apr 2019, 19:57 last edited by
            #5

            @SGaist I can use resize() only when there is no Layout. When I set the grid layout the resize does not work. When there are only two vertical widgets (in my example 1 and 3), it works perfectly.

            But, with all the four widgets:

            • If I only hide number 3, number 1 does NOT extend.
            • If I hide both number 3 and number 4 then number 1 and 2 extend and fill the empty place bellow them.

            It looks the grid system wants 1 and 2 be have same height.

            J 1 Reply Last reply 23 Apr 2019, 19:59
            0
            • B Bamshad
              23 Apr 2019, 19:57

              @SGaist I can use resize() only when there is no Layout. When I set the grid layout the resize does not work. When there are only two vertical widgets (in my example 1 and 3), it works perfectly.

              But, with all the four widgets:

              • If I only hide number 3, number 1 does NOT extend.
              • If I hide both number 3 and number 4 then number 1 and 2 extend and fill the empty place bellow them.

              It looks the grid system wants 1 and 2 be have same height.

              J Online
              J Online
              J.Hilk
              Moderators
              wrote on 23 Apr 2019, 19:59 last edited by J.Hilk
              #6

              @Bamshad there are a couple of ways to do this, Like @SGaist said, is one.

              If you don't force stretch factors and set the size policy to expanding, then you don't have to do anything. Like in this small example:

              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
              
                  QWidget w;
              
                  QVBoxLayout *vLay = new QVBoxLayout(&w);
                  QPushButton *btn = new QPushButton("Hide/Show");
              
                  QGridLayout *gLay = new QGridLayout;
                  QLabel *l = new QLabel("Widget 1");
                  l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                  l->setStyleSheet("background-color:red;");
                  gLay->addWidget(l,0,0,1,1);
              
                  l = new QLabel("Widget 2");
                  l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                  l->setStyleSheet("background-color:blue;");
                  QObject::connect(btn, &QPushButton::clicked, l, [l]()->void{l->setVisible(!l->isVisible());});
                  gLay->addWidget(l,0,1,1,1);
              
                  l = new QLabel("Widget 3");
                  l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                  l->setStyleSheet("background-color:yellow;");
                  QObject::connect(btn, &QPushButton::clicked, l, [l]()->void{l->setVisible(!l->isVisible());});
                  gLay->addWidget(l,1,0,1,1);
              
                  l = new QLabel("Widget 4");
                  l->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                  l->setStyleSheet("background-color:green;");
                  QObject::connect(btn, &QPushButton::clicked, l, [l]()->void{l->setVisible(!l->isVisible());});
                  gLay->addWidget(l,1,1,1,1);
              
                  vLay->addLayout(gLay,1);
                  vLay->addWidget(btn);
              
                  w.show();
              
                  return  a.exec();
              }
              

              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.

              1 Reply Last reply
              1
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 23 Apr 2019, 20:00 last edited by
                #7

                I didn't mention resize at any point. I wrote about the span values that you can use to make widgets use more than one column/row.

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

                B 1 Reply Last reply 23 Apr 2019, 20:10
                1
                • S SGaist
                  23 Apr 2019, 20:00

                  I didn't mention resize at any point. I wrote about the span values that you can use to make widgets use more than one column/row.

                  B Offline
                  B Offline
                  Bamshad
                  wrote on 23 Apr 2019, 20:10 last edited by
                  #8

                  @SGaist So, how can I change the span value? the widget is a scroll area.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 23 Apr 2019, 20:12 last edited by
                    #9

                    I already suggested a possible way to implement that, didn't I ?

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

                    B 1 Reply Last reply 23 Apr 2019, 22:13
                    1
                    • S SGaist
                      23 Apr 2019, 20:12

                      I already suggested a possible way to implement that, didn't I ?

                      B Offline
                      B Offline
                      Bamshad
                      wrote on 23 Apr 2019, 22:13 last edited by
                      #10

                      @SGaist Is it possible to see the columns and rows in the Design?

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 24 Apr 2019, 22:09 last edited by
                        #11

                        You'll see them once you add the widgets where you want them.

                        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
                        0

                        7/11

                        23 Apr 2019, 20:00

                        • Login

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