python pyqt5 application high dpi scaling

615 views Asked by At

We have built PyQt5 application using PyCharm IDE and executable created using pyinstaller. The application was built on a monitor with a display resolution of 1536 x 864, here the labels and buttons appear properly. But when the executable is run on a monitor with display resolution of 1920 x 1080 - the labels and buttons appear overlapping.

Based on the similar question on stackoverflow, I have tried the following suggestions

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)

the above status were added just before

APP = QApplication()

as well as

os.environ["QT_ENABLE_HIGHDPI_SCALING"]   = "1"
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
os.environ["QT_SCALE_FACTOR"]             = "1"

But on the higher resolution screen it is not showing the widgets properly

This issue is only seen when we run the executable file, if we run the project via PyCharm on the higher resolution screen it works fine.

Any suggestions on how to get the executables running on all screen resolutions.

Screenshots of low resolution display settings

enter image description here

And the UI run from the executable on this screen looks like

enter image description here

The date fields and the buttons are all visible clearly

Now the screenshots of high resolution laptop

enter image description here

And the UI run from the executable on this screen looks like

enter image description here

See the source code for the above UI

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtCore
import sys
import traceback

class BlankFormWidgetr(QWidget):
    def __init__(self, parent):
        super(BlankFormWidget, self).__init__(parent)
        self.init_UI()
        self.showMaximized()

    def init_UI(self):
        campaignList = ['Campaign 1', 'Campaign 2', 'Campaign 3']

        self.groupbox = QGroupBox("Get Statistics", self)
        self.groupbox.setFont(QFont('Arial', 10)
        self.groupbox.setAlignment(QtCore.Qt.AlignTop)
        self.groupbox.setFixedSize(750, 400)

        self.pagelayout = QVBoxLayout(self)
        self.pagelayout.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)

        self.labelFromDate = QLabel("From Date:", self)
        self.labelToDate = QLabel("To Date:", self)
        self.labelCampaign = QLabel("Campaign:", self)
        self.labelblankoutputwindow = Qlabel("", self)

        self.dateeditFromDate = QDateEdit(calenderPopup=True)
        self.dateeditFromDate.setDisplayFormat("dd-MMM-yyyy")
        self.dateeditFromDate.setFixedWidth(100)
        self.dateeditFromDate.setDateTime(QtCore.QDateTime.currentDateTime())

        self.dateeditToDate = QDateEdit(calenderPopup=True)
        self.dateeditToDate.setDisplayFormat("dd-MMM-yyyy")
        self.dateeditToDate.setFixedWidth(100)
        self.dateeditToDate.setDateTime(QtCore.QDateTime.currentDateTime())

        self.comboCampaign = QComboBox(self)
        self.comboCampaign.addItems(campaignList)
        self.comboCampaign.setFixedWidth(200)

        self.listboxOutput = QListWidget(self)
        self.listboxOutput.setFixedSize(250, 220)

        self.formlayout = QFormLayout(self)
        self.formlayout.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)
        self.formlayout.addRow(self.labelFromDate, self.dateeditFromDate)
        self.formlayout.addRow(self.labelToDate, self.dateeditToDate)
        self.formlayout.addRow(self.labelCampaign, self.comboCampaign)
        self.formlayout.addRow(self.labelblankoutputwindow, self.listboxOutput)

        self.buttongetmonthwisestats = QPushButton('Get Monthwise Stats', self)
        self.buttongetdailyweeklystats = QPushButton('Get Daily / Weekly Stats', self)
        self.buttongetnewordersstats = QPushButton('Get New Orders Stats', self)
        self.buttonGenerateReport = QPushButton('Generate Report', self)
        self.buttonReset = QPushButton('Reset', self)
        self.buttonClose = QPushButton('Close', self)

        self.buttonlayout = QHBoxLayout(self)
        self.buttonlayout.setAlignment(QtCore.Qt.AlignCenter)
        self.buttonlayout.addWidget(self.buttongetmonthwisestats)
        self.buttonlayout.addWidget(self.buttongetdailyweeklystats)
        self.buttonlayout.addWidget(self.buttongetnewordersstats)
        self.buttonlayout.addWidget(self.buttonGenerateReport)
        self.buttonlayout.addWidget(self.buttonReset)
        self.buttonlayout.addWidget(self.buttonClose)

        self.pagelayout.addLayout(self.formlayout)
        self.pagelayout.addLayout(self.buttonlayout)
        self.groupbox.setLayout(self.pagelayout)

        self.windowlayout = QVBVoxLayout(self)
        self.windowlayout.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop)
        self.windowlayout.addWidget(self.groupbox)
        self.show()

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__()
        self.blank_form_widget = BlankFormWidget(self)
        self.setCentralWidget(self.blank_form_widget)
        self.showMaximized()
        self.show()

if __name__ == '__main__':
    os.environ["QT_ENABLE_HIGHDPI_SCALING"]   = "1"
    os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
    os.environ["QT_SCALE_FACTOR"]             = "1"
    App = QApplication(sys.argv)
    if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
        App.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
    if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
        App.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)

    ex = MyMainWindow()
    App.exec_()
    App.quit()

The UI images has scroll bar but in code I have removed it - removing the scroll bar did not have any impact on the dpi issue.

0

There are 0 answers