Martí Climent
Martí Climent

Reputation: 557

QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True) not working at all on python

I've been programming an application with PySide2 and Python 3.8, and i was trying to enable HiDPi, and i found that adding this at the start of the script

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

was supposed to do the trick.

But it did not work for me. The window is shown with regular dpi (96ppp), while the whole system and apps are running on 125% (120ppp). Am I doing sth wrong?

Code:

import sys
from PySide2 import QtWidgets, QtGui, QtCore, QtMultimedia

"""
some functions
"""

if __name__ == "__main__":
    QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
    QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)

    app = QtWidgets.QApplication(sys.argv)

    window = QtWidgets.QMainWindow()
    label = QtWidgets.QLabel(window)
    label.setText("hey")
    window.show()
    
    app.exec_()

Screenshot (On the screenshot is difficult to appreciate the difference, I'm sorry): enter image description here

System specs: Windows 10 Pro 64bit Python 3.7.8 PySide2 5.15.2

Upvotes: 2

Views: 5128

Answers (3)

SodaCris
SodaCris

Reputation: 535

Try this:

import os
os.environ["QT_SCALE_FACTOR"] = "1.25"

before creating a QApplication

Also you can change the value to use other scale factors

Upvotes: 0

Joseph
Joseph

Reputation: 663

For PySide2 as far as I can tell you need to run from the QCoreApplication, setting attributes before initialising the app.

        QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
        QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
        app = QtWidgets.QApplication(sys.argv)

Upvotes: 4

Martí Climent
Martí Climent

Reputation: 557

I found the answer when testing the code on another computer. It seems like default Qt scaling alghoritm sets the scaling settings for 100-149% to 100% and 150% or + to 200% scaling.

I mean: If you have windows 10 scale set to 100% or 125%, it will be shown with 100% scaling

But instead, if you have windows 10 scale set to 150%, 175% or 200%, the window will be shown wit 200% scaling.

Upvotes: 1

Related Questions