PyQt GUI size on high resolution screens
Asked Answered
L

4

5

I posted a question a while ago asking about Tkinter backends and subsequently forgot about it but I've since realised that I'm using the pyqt backend. Is there a fix for that?

Original Question:

So it appears that matplotlib gui plots (a la plt.show()) don't adapt to monitor resolution and appear tiny on high resolution screens. Is there a matplotlib+Pyqt fix or do I have fiddle around somewhere in Windows settings?

Thanks

Example

Leclerc answered 31/8, 2016 at 10:39 Comment(0)
K
19

Before app is loaded, and not while loading.

insert:

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

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

like so :

import PyQt5
from PyQt5 import QtCore, QtGui, uic, QtWidgets
from PyQt5.QtWidgets import QWidget

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

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


class MyWindow(PyQt5.QtWidgets.QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()

And if that still don't work, in user en var and not system en var.

QT_AUTO_SCREEN_SCALE_FACTOR=2 

qt-5/highdpi

Kaph answered 8/12, 2017 at 23:10 Comment(2)
In case I start my app on a high DPI screen the solution works fine. In case I start the app on a low DPI screen the app window size is bigger and resized to the correct size after the application window has been moved with the mouse only. How ca I fix this?Patellate
... I had the minimumSize and maximumSize for width and height set to equal values to get a fiexed size app window. If I reset these properties in QtDesigner to the defaults the app windows are resized the right way. How can I prevent users from app window resizing now?Patellate
P
1

While I do not have much experience with PyQt, upon googling I found this SO question about setting the window state of a PyQT window to maximised. The accepted answer says to use self.showMaximized() to do so.

Plumbo answered 31/8, 2016 at 10:46 Comment(2)
That just maximises the window, not change the size of the gui elements/text etc. Now the window is just biggerLeclerc
@Leclerc Maybe this might help. #407439Plumbo
H
0

For python this worked for me:

if hasattr(Qt, 'AA_EnableHighDpiScaling'):
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
if hasattr(Qt, 'AA_UseHighDpiPixmaps'):
    QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    print(f"Using AA_EnableHighDpiScaling > {QApplication.testAttribute(Qt.AA_EnableHighDpiScaling)}")
    print(f"Using AA_UseHighDpiPixmaps    > {QApplication.testAttribute(Qt.AA_UseHighDpiPixmaps)}")
    # your window here
    app.exec()

based on this answer: https://mcmap.net/q/1098033/-pyqt-adjusting-for-different-screen-resolution-duplicate

Hebner answered 26/4, 2022 at 6:50 Comment(0)
F
0

To scale up your whole app, add these lines:

import os
os.environ["QT_SCALE_FACTOR"] = "1.2"  # Set scale to 120%
Famish answered 18/8 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.