7

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 ?

n0tis
  • 750
  • 1
  • 6
  • 27
  • 1
    The Qt documentation says to set that parameter as an environment variable. So you'll want to have a look at this for how to do it in Python: http://stackoverflow.com/q/5971312 make sure you do it before importing PyQt5. – three_pineapples Dec 26 '16 at 21:01
  • 1
    I have tried `os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"`, which didn't change anything. Then I found here http://bablabs.tistory.com/30 that I have to use `app.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)` additionally, but now i have the following error : `AttributeError: type object 'Qt' has no attribute 'AA_EnableHighDpiScaling'` – n0tis Dec 27 '16 at 09:44
  • 1
    That sounds like you're using an older Qt version which doesn't implement proper HighDPI support. – The Compiler Dec 27 '16 at 18:46
  • 1
    thanks, I'm using qt 5.4.1, and apparently hdpi support start with 5.6, I will try updating it asap :D – n0tis Dec 29 '16 at 14:08

3 Answers3

12

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)
Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
Jonathan
  • 6,741
  • 7
  • 52
  • 69
  • 2
    I am not sure this works for me. If I move the window to monitors set at different scaling values, the size of the content in the window fluctuate significantly. I expect the size of objects to appear about the same, or at least not truncate text in different windows. Is this functionality version dependent? Checking my versions as [described here](https://wiki.python.org/moin/PyQt/Getting%20the%20version%20numbers%20of%20Qt%2C%20SIP%20and%20PyQt), I have `QT_VERSION_STR: 5.9.4`, `PYQT_VERSION_STR: 5.9.2`, and `SIP_VERSION_STR: 4.19.8`. – Steven C. Howell Jul 31 '19 at 16:56
  • @StevenC.Howell if you right-click the executable on Windows you can check compatibility settings on DPI scaling and try various values and see if that fixes it. Try also `os.environ["QT_ENABLE_HIGHDPI_SCALING"] = "1"` – Jonathan May 17 '21 at 20:00
  • I am trying this within a QGIS plugin (Qt 5.11 & 5.15) and absolutely no joy, and I get crash after crash... Does anyone have any insights on this? – PCamargo Nov 23 '21 at 00:01
1

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.

Andew
  • 321
  • 1
  • 9
0

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.