PyQt 5 and 4k screen

15.4k views Asked by At

I'm trying to migrate an existing PyQt5 app to high-dpi for window 10.

The documentation of Qt5 itself speak about high-dpi scaling here: http://doc.qt.io/qt-5/highdpi.html

QT_AUTO_SCREEN_SCALE_FACTOR to "1".

But i can't adapt this in python code :/

Any idea ?

3

There are 3 answers

3
Jonathan On BEST ANSWER

Make sure you're on Qt 5.6+, skim the docs to be aware, then change your app init code to include one line before it and one line after it (written in Python) but it would be similar in C++:

os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
qapp = QApplication(sys.argv)
qapp.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
0
Patrick Pisciuneri On

This may be helpful to others who find this thread, as I did, when I ran into similar problems.

I was primarily developing a PyQt5 app on my Windows 10 laptop with a high dpi display. I noticed strange behavior when projecting my screen or moving the app to a different display in a dual monitor setup. Basically, font sizes would change dramatically, and that caused all kinds of problems with the layout.

Ultimately, reading about High DPI Desktop App Dev from Windows got me to the solution. They recommend using Per-Monitor (V2) DPI Awareness. It seems that Qt5 doesn't have support for this, but Qt6 does, and it is set appropriately by default.

So merely upgrading my dependency on PyQt5 to PyQt6 solved my problems. The version upgrade required some minor edits to my code. One minor hiccup was that QAction moved to the QtGui module from QtWidgets.

0
Andrew On

For me adding these three seemed to do the trick:

os.environ["QT_ENABLE_HIGHDPI_SCALING"]   = "1"
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
os.environ["QT_SCALE_FACTOR"]             = "1"

The "QT_SCALE_FACTOR" was the one that was influencing the scaling the most for me.