how to automatically resize widgets when window is resized without layout



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


  • Qt Champions 2016

    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.



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


  • Qt Champions 2016

    @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



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


  • Qt Champions 2016

    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 :)



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


  • Qt Champions 2016

    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?



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.