Scale Size of all GUI Widgets easily?



  • Hi,
    I have been developing an application while learning Qt.
    What I did was I hard coded the size of Widgets according what seemed good on my Laptop.
    Now I think that it might not be good idea, because if my user has a different screen resolution then the application will look different from what was intended.
    Now I know how to get the size of the user's screen resolution by QRect QDesktopWidget::availableGeometry().
    Now I can create scaling factors for height & width by:
    @hScale = (user's screen height) / (my screen height);
    wScale = (user's screen width) / (my screen width);@

    Now I wanted to know whether it is possible to apply these scales globally on all widgets by some function?
    Since I have created widgets in many classes & also with .ui file it would be very tedious to manually change the dimensions fo reach widget.
    Any way to simplify my problem?
    Thank You.


  • Moderators

    what is exactly your use case?!

    You should use "QLayouts":http://qt-project.org/doc/qt-4.8/layout.html which are handle the resizing of all child widgets for you. Then you can still restrict the size of your top level widget (e.g. the main window).



  • @raven-worx Yes I have used Layout's. But I hard coded the minimum size for some widgets like PushButton, ComboBox, etc.
    How do I solve my problem then?



  • if it is minimumSize then you can just leave it at that and just set your main windows size according to the availableGeometry and the widgets should expand to keep their look



  • You could go through all widgets with
    @QApplication::allWidgets()@
    and scale their minimum sizes accordingly. Make sure you test your UI for all realistic scale factors that will ever be used.



  • Hi DerManu I think that will solve my problems. But a few questions here:

    1. When should I call this function in my application at startup?
    2. What if widgets are added after calling this function?
    3. If I have to call this function every time I edit the GUI, then is there some way by which I can skip those widgets that are already scaled?
    4. Is this method efficient, in terms of time & computation?
      Thank You.

  • Moderators

    [quote author="CAD_coding" date="1366792630"]1. When should I call this function in my application at startup?[/quote]
    on every resize of your mainwindow.

    [quote author="CAD_coding" date="1366792630"]2. What if widgets are added after calling this function?[/quote]
    so they are not included in the list at the time it is called.

    [quote author="CAD_coding" date="1366792630"]3. If I have to call this function every time I edit the GUI, then is there some way by which I can skip those widgets that are already scaled?[/quote]
    you could set a dynamic (int) property on the widgets which increases on every iteration.

    [quote author="CAD_coding" date="1366792630"]4. Is this method efficient, in terms of time & computation?[/quote]
    No it's not... depending at the number of widgets you have in your application. But in general your approach is bad design IMHO.



  • By the way, you do know about horizontal/vertical scale factors for widgets in layouts? Kind of sounds like you're trying to reinvent the wheel a bit. I do see the necessity to have different parameters for very different screen sizes, but continuously modifying the minimum size on every resize event of the main window, sounds like there is something wrong. Scalefactors might do something similar for you, without the hackery.


Log in to reply
 

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