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. Automatically resize the widget

Automatically resize the widget

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 3.5k 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.
  • NevezN Offline
    NevezN Offline
    Nevez
    wrote on last edited by
    #1

    Hello , I have a QWidget that I created. I placed a layout in this widget and there are buttons in this layout.
    What I want to do is:
    The horizontal length of my widget is equal to my entire mainwindow, and
    When I enlarge or shrink my mainwindow, my widget is sized accordingly.
    How can I do that ?

    
    QWidget *widget= new QWidget(this);
    QPushButton *btn1 = new QPushButton();
    QPushButton *btn2 = new QPushButton();
    QPushButton *btn3 = new QPushButton();
    QHBoxLayout *horizontal_bottom = new QHBoxLayout();
    
      horizontal_bottom->setSpacing(0);
      horizontal_bottom->addWidget(btn1 );
      horizontal_bottom->addWidget(btn2);
      horizontal_bottom->addWidget(btn3);
      horizontal_bottom->setContentsMargins(0,0,0,0);
      widget->setLayout(horizontal_bottom);
    
    jsulmJ 1 Reply Last reply
    0
    • NevezN Nevez

      Hello , I have a QWidget that I created. I placed a layout in this widget and there are buttons in this layout.
      What I want to do is:
      The horizontal length of my widget is equal to my entire mainwindow, and
      When I enlarge or shrink my mainwindow, my widget is sized accordingly.
      How can I do that ?

      
      QWidget *widget= new QWidget(this);
      QPushButton *btn1 = new QPushButton();
      QPushButton *btn2 = new QPushButton();
      QPushButton *btn3 = new QPushButton();
      QHBoxLayout *horizontal_bottom = new QHBoxLayout();
      
        horizontal_bottom->setSpacing(0);
        horizontal_bottom->addWidget(btn1 );
        horizontal_bottom->addWidget(btn2);
        horizontal_bottom->addWidget(btn3);
        horizontal_bottom->setContentsMargins(0,0,0,0);
        widget->setLayout(horizontal_bottom);
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Nevez said in Automatically resize the widget:

      widget->setLayout(horizontal_bottom);

      Did you also add this widget to a layout?

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

      1 Reply Last reply
      0
      • NevezN Offline
        NevezN Offline
        Nevez
        wrote on last edited by
        #3

        I added it but nothing changed.

        QGridLayout *finalLayout = new QGridLayout(this);
          finalLayout->addWidget(widget);
        

        i tried that too.

        QHBoxLayout *horizontal_bottom = new QHBoxLayout(widget);
        
        jsulmJ 1 Reply Last reply
        0
        • F Offline
          F Offline
          Fabien-B
          wrote on last edited by
          #4

          Is your widget the central widget of the Window ? How did you add it ? I think your code should works.

          NevezN 1 Reply Last reply
          0
          • F Fabien-B

            Is your widget the central widget of the Window ? How did you add it ? I think your code should works.

            NevezN Offline
            NevezN Offline
            Nevez
            wrote on last edited by Nevez
            #5

            @Fabien-B
            no it's not centralwidget. Another widget is set as CentralWidget. If I set this widget as centralWidget, the application crashes . Is there any alternative option?

            1 Reply Last reply
            0
            • NevezN Nevez

              I added it but nothing changed.

              QGridLayout *finalLayout = new QGridLayout(this);
                finalLayout->addWidget(widget);
              

              i tried that too.

              QHBoxLayout *horizontal_bottom = new QHBoxLayout(widget);
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Nevez said in Automatically resize the widget:

              QHBoxLayout *horizontal_bottom = new QHBoxLayout(widget);

              Where is this layout set?
              You need to add widget to a layout on your central widget.

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

              1 Reply Last reply
              0
              • NevezN Offline
                NevezN Offline
                Nevez
                wrote on last edited by
                #7

                @jsulm
                I added a widget to the layout in my central widget. Horizontal length and scaling is exactly what I wanted. But this time, I can't move the widget I added to the position I want (vertically).
                The widget just attaches to the center and does not move.
                Is there a way to change its location?

                jsulmJ 1 Reply Last reply
                0
                • NevezN Nevez

                  @jsulm
                  I added a widget to the layout in my central widget. Horizontal length and scaling is exactly what I wanted. But this time, I can't move the widget I added to the position I want (vertically).
                  The widget just attaches to the center and does not move.
                  Is there a way to change its location?

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

                  @Nevez Then first add a vertical layout and then your horizontal layout

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

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    Fabien-B
                    wrote on last edited by
                    #9

                    The position and size of your widget is handled by the layout that contains it.
                    If you want your widget to be "pushed" to the top, the best is to add a spacer after your widget.

                    You can do it that way:

                    QVBoxLayout *vbox = new QVBoxLayout(this);
                    vbox->addWidget(widget);
                    vbox->addStretch(1);
                    

                    See https://doc.qt.io/qt-5/qboxlayout.html#addStretch for more details.

                    NevezN 1 Reply Last reply
                    0
                    • F Fabien-B

                      The position and size of your widget is handled by the layout that contains it.
                      If you want your widget to be "pushed" to the top, the best is to add a spacer after your widget.

                      You can do it that way:

                      QVBoxLayout *vbox = new QVBoxLayout(this);
                      vbox->addWidget(widget);
                      vbox->addStretch(1);
                      

                      See https://doc.qt.io/qt-5/qboxlayout.html#addStretch for more details.

                      NevezN Offline
                      NevezN Offline
                      Nevez
                      wrote on last edited by
                      #10

                      @Fabien-B So how can I do this if I use QGridLayout instead of QHBoxLayout? Thanks.

                      1 Reply Last reply
                      0
                      • F Offline
                        F Offline
                        Fabien-B
                        wrote on last edited by
                        #11

                        If I understood your problem correctly, you should have 3 layouts:
                        A QHBoxLayout which contains your buttons (that will span the full width of you window), a QGridLayout that contains the rest of your stuff, and a QVBoxLayout, that contains the other layouts plus optionally a spacer to "push" your buttons at the top (or the bottom).

                        You could have something like that for example :

                        147bc526-1b1f-4de1-9552-8b6196dd026f-image.png

                        Try using QtCreator or QtDesigner to understand the layouts, even if in the end you make it all with code.

                        1 Reply Last reply
                        1
                        • NevezN Offline
                          NevezN Offline
                          Nevez
                          wrote on last edited by
                          #12

                          @Fabien-B Thank you very much for your interest and help. I checked the part you mentioned. However, unfortunately, the project I am working on has reached a certain size and I am progressing through the code. However, I still managed to fix the problem.

                          I was able to push down the widget I wanted to add using "QSpacerItem" as a solution. Then again, there was an irregularity with the other widgets I added. I managed to solve this problem by giving the row and column values ​​different when adding the widgets to the layout. I hope this will be of use to someone. Have a nice day :)

                          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