QWidget Scaling



  • How would one go about introducing scaling to widgets such as a QPushButton when running on low/high resolution displays?

    For example, an application we have has a side menu widget running vertically down the left side of the screen. Buttons in this widget are layed out vertically and are specified as 64x64px in size. On a high-res display, this looks great but customers running on low-res (1024x768) displays are losing buttons off the bottom of the screen.

    Would would be really nice is to to be able to scale between the minimumSize and maximumSize depending on the overall size of the Application window: at low-res 1024x768, the buttons may reduce to 32x32px whereas from 1920x1080 the buttons increase to 64x64px.

    This would be massively helpful on tablets/phones where the resolutions also vary wildly.

    Please tell me a solution already exists, I just don't know about it! Thanks :)

    Also, the widget would need to maintain a certain aspect ratio. So if the min is 32x32 and max is 64x64, the widget would never achieve a size of say 42x48 but 48x48 would be valid.


  • Lifetime Qt Champion

    Hi,

    Usually, with widgets you use layouts.



  • @SGaist having used layouts extensively in my application, I’m yet to find any properties of any layout which would allow me to achieve the above.


  • Lifetime Qt Champion

    If you want very specific sizes like that then you have to implement that yourself. Layouts try to make the best with whatever is available at runtime suiting the platform you are running on.


  • Qt Champions 2017

    Hi
    You could implement your own custom widget
    http://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-example.html
    that scales like that.
    Or simply use a function that directly sets the size of the buttons on/before show.
    Im pretty sure if u made a aspect ratio aware layout, it would be useful to to others.
    Its asked often. :)



  • See http://doc.qt.io/qt-5/highdpi.html and http://blog.qt.io/blog/2016/01/26/high-dpi-support-in-qt-5-6/

    The easiest way, if you are not on Apple's devices, is: program for low resolution and add QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); before you call QApplication's constructor


Log in to reply
 

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