How to setup a pyqt GUI to work on mulitple display sizes and resolutions?

Viewed 28

I have a pyqt GUI I want to distribute. I am working on windows on a full HD display and the current version of my GUI looks fine on my screen. But on other screens with a resolution of, for instance 4k, the GUI looks far too small and is barely readable. I suppose this is the case because I used fixed coordinates. I was thinking about using coordinates relative to the user's display size. Imagine I have an approach for this solution like this:

app = QApplication(sys.argv)
screen = QApplication.instance().primaryScreen().availableGeometry()
main_window = QMainWindow()
main_window.setObjectName(u"MainWindow")
main_window.show()
width = screen.width()
height = screen.height()
main_window.resize(int(0.2*width),int(0.1*height))
app.exec()

And for the positioning of the widgets respectively:

label.setGeometry(QRect(x_pos, y_pos, int(0.2*width),int(0.1*height)))

Is this the right way to do it? Especially I was wondering whether there is something built in in pyqt which I could use for such a common task. During my research I found something like:

QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)

or also something with

os.environ["QT_FONT_DPI"] = "96"

But I don't really know how and if I should use something like this at all.

Is my way the correct approach to this problem or is there a simpler solution?

Thanks in advance.

0 Answers
Related