I am using PyQt5 and Python 3.6.4 to design a ui for a program. It was made on a 720p monitor however now using the same code on a 4k monitor, everything is tiny apart from the text. How would I go about resizing the whole app to look the same on all monitors: (720p, 1080p, 4k, etc.) The program is to be run on windows through an executable created through compiling the python code. Cheers
2 Answers
Simple 1 line fix for any who need
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"

- 329
- 6
- 19
This is somewhat system dependent, so it would help if you mentioned your target platform(s).
Because PyQt5 is just a wrapper around Qt5, I think High DPI Displays from the Qt manual applies. Citing the relevant bit (but you should read the whole thing):
In order to get an application designed for low DPI values running on a high resolution monitors quickly, consider one of the scaling options (let the application run as DPI Unaware on Windows or set the environment variable QT_AUTO_SCREEN_SCALE_FACTOR to "1". These options may incur some scaling or painting artifacts, though.
In the longer term, the application should be adapted to run unmodified:
- Always use the qreal versions of the QPainter drawing API.
- Size windows and dialogs in relation to the screen size.
- Replace hard-coded sizes in layouts and drawing code by values calculated from font metrics or screen size.
In a shell, you would do something like:
$ export QT_AUTO_SCREEN_SCALE_FACTOR=1
$ python my_application.py
-
Please see edits. Also please note that the program is to be run by people with no programming knowledge and therefore I would require the program to set up the resizing on its own. – Kermit Mar 01 '18 at 11:17
-
This is why I advised you to read the entire article :) It's too much to cite here. The environment var is just to test whether it works. – Thomas Mar 01 '18 at 11:18
-
Ok will do. Thanks – Kermit Mar 01 '18 at 11:20
-
Thanks for the link. I ended up getting it to work by setting the QT_DEVICE_PIXEL_RATIO to auto. This is said to be deprecated but im not sure why. It seems to work well. – Kermit Mar 01 '18 at 11:45
-
I think you're supposed to set `Qt::AA_EnableHighDpiScaling` instead. In C++, it would be `myApplication.setAttribute(Qt::AA_EnableHighDpiScaling);`, not sure how exactly that translates to Python. – Thomas Mar 01 '18 at 13:21
-
Your solution worked but oddly only through setting the variable through the script. Cheers – Kermit Mar 01 '18 at 16:13