Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Scale Size of all GUI Widgets easily?
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?
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?
b1gsnak3 last edited by
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
DerManu last edited by
You could go through all widgets with
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:
- When should I call this function in my application at startup?
- What if widgets are added after calling this function?
- 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?
- Is this method efficient, in terms of time & computation?
[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.
DerManu last edited by
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.
mrX2yyZaD6 last edited by
I have a Qt application where I sometime need to rescale (as opposed to resizing using layouts) all the widgets for screens of different
display resolution. I did a tree walk of every widget and adjusted the minimum size, the maximum size, the current size and the font size
for each widget. Other widgets got special attention, such as the indent for a QLabel widgets. The scaling could be a one off application
wide scaling or more dynamic and applied to an individual form (in response to ctrl/+ and ctrl/- like most browsers).
In order not to accumulate scaling errors, the original size values are saved in a dynamic property associated with each widget.
For application wide scaling, the scaling is applied after each ui file is loaded or widget created.
Have a look at https://github.com/qtepics/qeframework/blob/master/qeframeworkSup/project/common/QEScaling.h and .cpp file.
It would not take much effort to decouple this class to make stand alone.