0

So I made a PyQt5 app and it works on one computer but when I try to run it on another one with a bigger resolution it doesn't work. I tried:

PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)

but it just makes the window bigger, without that line the window is a little smaller and the font size is messed up.

high resolution without command: enter image description here

high resolution with command: enter image description here

low resolution and low resolution without command works the same and it looks like this(normal): enter image description here

Marko
  • 157
  • 1
  • 2
  • 8
  • What you mean with "it doesn't work"? Can you provide images of both situations? – musicamante Apr 06 '21 at 16:32
  • I am not able to provide images right now but when I run it on a computer with a smaller resolution(1680x1050) it appears normal but on higher resolutions, the window is smaller and font size is bigger if I do not use the command above, the window is even bigger and the font size is normal. – Marko Apr 06 '21 at 16:43
  • That could depend on lots of factors (including the font scaling of the system). Also, if you're trying to set custom fonts (and/or their sizes) that could create issues. With "window is smaller" do you mean that "it looks smaller" or that it actually has a different **pixel** size? Have you read the documentation about [High DPI)[https://doc.qt.io/qt-5/highdpi.html] support (there's a section that explains how to set environment variables and refers to font sizes too). Without any image/code reference and actual screen configuration information, is a bit hard to help you. – musicamante Apr 06 '21 at 16:50
  • Also consider that with modern system and High DPI support, some windows/widgets look "normal", but they actually are much bigger in pixel geometries. – musicamante Apr 06 '21 at 16:51
  • The window appears smaller, it has the same pixel size, I will try to take images of both cases and I will post them in about 20 minutes. – Marko Apr 06 '21 at 16:53
  • I edited the question and added images. – Marko Apr 06 '21 at 17:03
  • I see a lot of errors in the log regarding setting invalid font sizes, how are you trying to do that? – musicamante Apr 06 '21 at 17:07
  • I probably set the point size to 0 somewhere in the qt designer but I don't think it affects this problem. Since the app is not terminating I do not think it is an error. – Marko Apr 06 '21 at 17:11
  • 2
    It's not a fatal error, but since your problem is with font scaling it might be an important hint, especially considering that it doesn't appear just once. I suggest you to create a *small* example Ui that only implements *basic* elements (not your custom ones), and then do some tests with the different configurations to better understand how it works; then try adding some of your custom widgets and see what happens. Scaling is a very hard thing to implement, doing it with an already completed Ui is even harder, and we cannot really help you if we don't know how you're dealing with font sizes. – musicamante Apr 06 '21 at 17:17

2 Answers2

0

I had this bug and I have fixed it. But I'm not sure this will 100% work as when you tried:

 PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)

It didn't work.

But for me, wrapping it in an if statement worked:

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)

Give it a go, but I'm not 100% certain it'll work.

0

Let's try the following steps to fix the problem: First) let's suppose the recommended dispaly screen (DS) is 150% (or DS="1.5"). The display screen gets back to the PC by which your Python code has been written. Second) Add the following script at the begining of your program (before your program starts to run)

def suppress_qt_warnings():
    DS="1.5"

    scaleFactor = str(ctypes.windll.shcore.GetScaleFactorForDevice(0) /100)
    if float(scaleFactor)>float(DS):
        scaleFactor=DS

    environ["QT_DEVICE_PIXEL_RATIO"] = "0"
    environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "0"
    environ["QT_SCALE_FACTOR"] = "1"
    environ["QT_SCREEN_SCALE_FACTORS"] = scaleFactor


suppress_qt_warnings()

if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
    PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
    # enable highdpi scaling

if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
    PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
    # use highdpi icons


if __name__ == "__main__":      

    app = QtWidgets.QApplication(sys.argv)