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 automatically resize widgets when window is resized without layout
QtWS25 Last Chance

how to automatically resize widgets when window is resized without layout

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 14.1k 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.
  • DannyL408D Offline
    DannyL408D Offline
    DannyL408
    wrote on last edited by
    #1

    I want to resize my widgets and all the widgets in my widget automatically everytime without hardcoding it. is that possible.
    (i dont want to use a layout because i dont want my widgets to move around automatically)

    Ex( mainwindow is 1200x900)

    i have a qframe that is 400x300 in the middle with a bunch of widgets inside with qframe as parent (lets saythey are all 25x25 boxes in various places of this qframe.

    when i resize my mainwindow to 900x750 (aka 75%) i want to have my qframe at 300,225 size but also maintain that its in the "middle" of the screen relative to the main window size, and also have the child widgets of qframe auto resize to 75% and have them in the same position relative to qframe.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Layout is the way to go. There are many options to adjust how they resize.
      Manually will not be easy.

      Anyway, you can override
      void MainWindow::resizeEvent(QResizeEvent* event)
      and then use
      http://doc.qt.io/qt-5/qobject.html#findChildren
      to get all childs and to what ever adjustments you want using
      setGeometry

      The QResizeEvent contains both old and new size so you can calculate grows diff.

      DannyL408D 1 Reply Last reply
      4
      • mrjjM mrjj

        Hi
        Layout is the way to go. There are many options to adjust how they resize.
        Manually will not be easy.

        Anyway, you can override
        void MainWindow::resizeEvent(QResizeEvent* event)
        and then use
        http://doc.qt.io/qt-5/qobject.html#findChildren
        to get all childs and to what ever adjustments you want using
        setGeometry

        The QResizeEvent contains both old and new size so you can calculate grows diff.

        DannyL408D Offline
        DannyL408D Offline
        DannyL408
        wrote on last edited by DannyL408
        #3

        @mrjj hey thanks for the quick reply,
        im currently doing it manually by having my mainwindow connect a resizeevent signal to all the child widgets (but that seems very tedious since i have over 20 widgets to resize and reposition)

        the problem with using layouts is for example i have like a 250x250 layout
        and i just want a 50x50 qframe in the top left and a 50x50 qframe in the bottom right, i cant have the rest jsut be "empty or position them absolutely" . Then when i resize them i have to resize the widget with the layout but the widgets inside the layout dont resize with the same ratio as the original.

        The main problem i have with layouts is they will try to grow in a certain area/stretch

        mrjjM 1 Reply Last reply
        0
        • DannyL408D DannyL408

          @mrjj hey thanks for the quick reply,
          im currently doing it manually by having my mainwindow connect a resizeevent signal to all the child widgets (but that seems very tedious since i have over 20 widgets to resize and reposition)

          the problem with using layouts is for example i have like a 250x250 layout
          and i just want a 50x50 qframe in the top left and a 50x50 qframe in the bottom right, i cant have the rest jsut be "empty or position them absolutely" . Then when i resize them i have to resize the widget with the layout but the widgets inside the layout dont resize with the same ratio as the original.

          The main problem i have with layouts is they will try to grow in a certain area/stretch

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @DannyL408
          Oh, you are already doing it manually. ( yes its tedious )

          • The main problem i have with layouts is they will try to grow in a certain area/stretch

          Well you can limit that with min/maxSize and Spacers.

          Hard to say if your use case is possible/ can work good
          but if all your layout is same type. (rects in opposite corners )

          Maybe you can write your own layout class and simply use that?
          http://doc.qt.io/qt-5/qtwidgets-layouts-borderlayout-example.html

          1 Reply Last reply
          3
          • DannyL408D Offline
            DannyL408D Offline
            DannyL408
            wrote on last edited by
            #5

            @mrjj said in how to automatically resize widgets when window is resized without layout:

            @DannyL408
            Oh, you are already doing it manually. ( yes its tedious )

            • The main problem i have with layouts is they will try to grow in a certain area/stretch

            Well you can limit that with min/maxSize and Spacers.

            Hard to say if your use case is possible/ can work good
            but if all your layout is same type. (rects in opposite corners )

            Maybe you can write your own layout class and simply use that?
            http://doc.qt.io/qt-5/qtwidgets-layouts-borderlayout-example.html

            Spacers! that must be the solution, thank you! nothing i googled gave me something like that to add some pseudo "absolute" positioning.
            (i currently am playing around with layouts in my current widget, i have a setup like this

            A 525,450 QFrame (this is the widget)
            HLayout that covers this frame
            a 75,450 QFrame(left side)
            added to HLAYOUT
            V Layout added to left side QFrame with 6 push buttons.
            a 450,450 QFrame(right side)
            V Layout added to Qframe right side
            STacked widget added to the layout

            tldr; the right side doesnt resize properly after i add a custom QFramewidget to the stacked widget, idk why

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

              Hi
              So it works until you add a custom QFramewidget ?
              Is that by promotion ?
              Sadly I have no idea what could be wrong as debugging layouts talking about them is
              hard.

              I tried to follow
              A 525,450 QFrame (this is the widget)
              HLayout that covers this frame
              a 75,450 QFrame(left side)
              added to HLAYOUT
              ..

              In Designer to see what you have but didn't get it right.

              A picture could tell alot but not why layout being funky :)

              1 Reply Last reply
              1
              • DannyL408D Offline
                DannyL408D Offline
                DannyL408
                wrote on last edited by
                #7

                @mrjj said in how to automatically resize widgets when window is resized without layout:

                Hi
                So it works until you add a custom QFramewidget ?
                Is that by promotion ?
                Sadly I have no idea what could be wrong as debugging layouts talking about them is
                hard.

                I tried to follow
                A 525,450 QFrame (this is the widget)
                HLayout that covers this frame
                a 75,450 QFrame(left side)
                added to HLAYOUT
                ..

                In Designer to see what you have but didn't get it right.

                A picture could tell alot but not why layout being funky :)

                I think I will try a different approach.
                Is there a way to set sizehint's for qt's default widgets without having to inherit and reimplement the sizehint function?
                I want to be able to use layouts to rescale my widgets but maintain its form.

                Ex: i have a frame of 100x100 widgets in a 1080p resolution
                i want it to maintain a square form no matter what resolution i have it (or will i have to hardcode this?)

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  hi
                  Hmm, I do not hink you can catch with eventfiler
                  so I think subclassing the only way to override sizehint.
                  Are you using many different widgets since subclassing it is not an option?

                  DannyL408D 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    hi
                    Hmm, I do not hink you can catch with eventfiler
                    so I think subclassing the only way to override sizehint.
                    Are you using many different widgets since subclassing it is not an option?

                    DannyL408D Offline
                    DannyL408D Offline
                    DannyL408
                    wrote on last edited by
                    #9

                    @mrjj said in how to automatically resize widgets when window is resized without layout:

                    hi
                    Hmm, I do not hink you can catch with eventfiler
                    so I think subclassing the only way to override sizehint.
                    Are you using many different widgets since subclassing it is not an option?

                    I am using many difference widgets, i was just curious if there was a way to override sizehints on the default widget classes.

                    Thanks for all your help ill be able to iron out the kinks

                    1 Reply Last reply
                    1

                    • Login

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