Qt Widgets Application scaling on different resolution screens
-
Hi! I have developed a Qt Widgets Application which works prefectly fine when displayed on a 4k resolution screen (my native screen res). I have noticed scaling problems when launching the application on screens with lower resolutions: 2K, 1080p, etc. Instead of maintaining the proportions, the program becomes too big and its bottom part is inaccessible, as it becomes hidden under the Windows task bar. Is there a way to force proper scaling with multiple resolutions?
-
Hi, I also had this problem for my widgets apps on Windows, but there are some settings you can apply at program start that will help. If you are using Qt 5.14 or later, try adding these 2 lines:
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
-
I have tried with
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
But that didn't solve the issue (in some scenarios even made it worse). Right now I have tested your suggestion by adding also
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
Unfortunately it still doesn't fit on screens with resolution lower than 4k. I am thinking it is my layouts and widgets that could potentially be the source of the issue.
-
Ok. It's tricky with the different resolutions/screen sizes on Windows.
Since most widgets are about 20 years old, they are meant to be run on a PC with 1920x1080 resolution and a scale factor of 100%. That means you have to run Qt Creator and design your widgets for such an environment, in particular, using any other scale factor than 100% will always spell trouble :-(
(For example, vertical scroll bars have a hardwired/fixed size of 16 pixels etc.)Then to get my apps running nicely on a normal display nowadays (4K and 200% screen scaling) I apply those settings I showed above. On my dev. PC they don't make any difference, but on a PC with for example 200% screen scaling they will make Qt render its widgets as if the screen still had a 100% screen scaling. Windows will then upscale my widgets so that they look similar to other non-Qt windows on the screen.
.
The upscaling done by Windows will not look as smooth/nice as a real, modern high-DPI aware app, but it's better than having most of the QLabels etc. only showing half of their letters. -
For future reference:
In my experience, having your QtWidgets-application scale properly on different DPI screens and scale factors requires a few things:
(Based on Qt 5.9, there might be a better way in newer versions)
- Setting the environment variable QT_AUTO_SCREEN_SCALE_FACTOR to TRUE
- Using only point-sizes for fonts
- If you have to define a size for a widget (no layout to handle it for you), using only realtive units (e.g. "em", which is a unit that scales relative to the font size) in the widgets stylesheets helps. Example: QPushButton { min-width: 3.2em; }.
- Use only vector graphics for icons, since they scale infinitely up and down without aliasing
With this, I got our application to look good on most of our screens, with wildly ranging DPI values and Windows scale factors.
There still are ouliers, though: Widescreen displays still pose an issue. My guess would be that the algorithm for scaling was created at a time where these monitors didn't exist yet and it can't handle such strange dimensions. Also, it would work perfectly fine on one 4K TV with 300% scaling, while it didn't on a different 4K TV with 300% scaling.The High-DPI experience with Qt is really frustrating, to say the least. We are about to update to the latest long-term release and I HOPE something has changed. Although I'm not too optimistic.