PyQt - Adjusting for Different Screen Resolution [duplicate]
Asked Answered
T

1

10

I've developed an application using PyQt on a computer that has a resolution of 3840x2160. I used QtDesigner to create almost all of my UI needs, then added the few other things I needed manually in python. Once I went to deploy the application, I realized that the target computer had a resolution of 1920x1080. Some widgets in the application (generally those that just had text/buttons) are fine, but once I start to get in to more complex layouts things don't appear as clearly. Specifically, QLabels and other QItems overlap each other, images are scaled disproportionately along their horizontal axis, and spacing between objects decreases. The following are a couple of images that illustrate the problem.

3480x2160 Screen (250% Scaling according to Windows Scale and Layout) Note: This is what I see when I am developing.

3480x2160 Screen (100% Scaling according to Windows Scale and Layout)

1920x1080 Screen (100% Scaling according to Windows Scale and Layout)

I also noticed that Qt Designer itself has a similar problem when transitioning to a different screen resolution (see images attached).

High Resolution Screen - QtDesigner

Lower Resolution Screen - QtDesigner

What is the proper way to go about dealing with multiple screen resolutions in PyQt if I want to leverage QtDesigner? I know that I can retrieve the screen resolution with QDesktopWidget.screenGeometry, but is there any way to scale every parameter at once rather than manually changing each item? Will avoiding a particular "Size Policy" help at all with this?

Telemetry answered 11/5, 2017 at 0:23 Comment(4)
Could you check that this is not (perhaps partially) due to a different screen scaling configured in windows? (see pcworld.com/article/2953978/displays/… )Felucca
You aren't wrong that screen scaling makes a difference in the appearance of the application but it doesn't appear to be the solution.Telemetry
What do you mean "things don't appear as clearly"? What exact problem are you trying to solve? Images are fixed pixel dimensions, you usually supply multiple images with increasing dimensions and shrink (because shrinking looks a lot better than zooming) the closest one that is equal or larger than your target widget dimensions.Ceremonial
Thank you for pointing out that it wasn't clear; I updated the question to illustrate the issues more clearly but let me know if there is more I should elaborate on.Telemetry
S
7

If the issues is more with different dpi than resolution, you can tell Qt to use high dpi scaling/pixmaps by adding the following lines to your application before you create your QApplication (or any other Qt classes):

# Handle high resolution displays:
if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
    QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
    QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)

I have found that this makes my applications behave the same (or at least more similar) when switching between my laptop (high dpi, high resolution) and desktop (normal dpi, 1920x1080).

I don't use QtDesigner, so I have no idea how this suggestion plays with QtDesigner.

Sebaceous answered 15/5, 2019 at 0:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.