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.
  • N Offline
    N Offline
    Nevez
    wrote on 17 Feb 2022, 12:06 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);
    
    J 1 Reply Last reply 17 Feb 2022, 12:24
    0
    • F Offline
      F Offline
      Fabien-B
      wrote on 17 Feb 2022, 12:13 last edited by
      #4

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

      N 1 Reply Last reply 17 Feb 2022, 12:17
      0
      • F Fabien-B
        17 Feb 2022, 12:13

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

        N Offline
        N Offline
        Nevez
        wrote on 17 Feb 2022, 12:17 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
        • N Nevez
          17 Feb 2022, 12:06

          I added it but nothing changed.

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

          i tried that too.

          QHBoxLayout *horizontal_bottom = new QHBoxLayout(widget);
          
          J Online
          J Online
          jsulm
          Lifetime Qt Champion
          wrote on 17 Feb 2022, 12:24 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
          • N Offline
            N Offline
            Nevez
            wrote on 17 Feb 2022, 12:28 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?

            J 1 Reply Last reply 17 Feb 2022, 12:31
            0
            • N Nevez
              17 Feb 2022, 12:28

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

              J Online
              J Online
              jsulm
              Lifetime Qt Champion
              wrote on 17 Feb 2022, 12:31 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 17 Feb 2022, 12:42 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.

                N 1 Reply Last reply 17 Feb 2022, 13:20
                0
                • F Fabien-B
                  17 Feb 2022, 12:42

                  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.

                  N Offline
                  N Offline
                  Nevez
                  wrote on 17 Feb 2022, 13:20 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 17 Feb 2022, 13:44 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
                    • N Offline
                      N Offline
                      Nevez
                      wrote on 17 Feb 2022, 14:36 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

                      12/12

                      17 Feb 2022, 14:36

                      • Login

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