6

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
  • 688
  • 1
  • 5
  • 25
  • 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

1 Answers1

11

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
  • 28,884
  • 9
  • 105
  • 105
Jonathan
  • 6,038
  • 7
  • 44
  • 66
  • 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