w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Making Qt Application that work with different System Font Sizes

You don't: you let Qt do the job for you. Use layouts to arrange your widgets; avoid setting fixed sizes. Last point: I recommend to use Qt-Designer to create your interfaces.

Edit for Dmitry: here are Python files from 2 ui files (generated with pyuic4) each one with 2 QLabel. 1 Qlabel font is huge (72), the other is the default one (10).

  1. Min and Max size are constrained: here if max font size is 10, the label is correctly displayed.

    from PyQt4 import QtCore, QtGui
    
    class Ui_Dialog(object):
        def setupUi(self, Dialog):
            Dialog.setObjectName("Dialog")
            Dialog.resize(115, 160)
            sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed,
    QtGui.QSizePolicy.Fixed)
            sizePolicy.setHorizontalStretch(0)
            sizePolicy.setVerticalStretch(0)
           
    sizePolicy.setHeightForWidth(Dialog.sizePolicy().hasHeightForWidth())
            Dialog.setSizePolicy(sizePolicy)
            Dialog.setMinimumSize(QtCore.QSize(115, 160))
            Dialog.setMaximumSize(QtCore.QSize(115, 160))
            self.verticalLayout = QtGui.QVBoxLayout(Dialog)
            self.verticalLayout.setObjectName("verticalLayout")
            self.label = QtGui.QLabel(Dialog)
            font = QtGui.QFont()
            font.setFamily("Andale Mono")
            font.setPointSize(72)
            self.label.setFont(font)
            self.label.setObjectName("label")
            self.verticalLayout.addWidget(self.label)
            self.label_2 = QtGui.QLabel(Dialog)
            self.label_2.setObjectName("label_2")
            self.verticalLayout.addWidget(self.label_2)
    
            self.retranslateUi(Dialog)
            QtCore.QMetaObject.connectSlotsByName(Dialog)
    
        def retranslateUi(self, Dialog):
            Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog",
    "Dialog", None, QtGui.QApplication.UnicodeUTF8))
            self.label.setText(QtGui.QApplication.translate("Dialog", "UGLY",
    None, QtGui.QApplication.UnicodeUTF8))
            self.label_2.setText(QtGui.QApplication.translate("Dialog", "Not
    ugly", None, QtGui.QApplication.UnicodeUTF8))
    
    
    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        Dialog = QtGui.QDialog()
        ui = Ui_Dialog()
        ui.setupUi(Dialog)
        Dialog.show()
        sys.exit(app.exec_())
    
  2. No more constraint on size: the label with font size 72 can be displayed.

    from PyQt4 import QtCore, QtGui
    
    class Ui_Dialog(object):
        def setupUi(self, Dialog):
            Dialog.setObjectName("Dialog")
            Dialog.resize(495, 140)
            self.verticalLayout = QtGui.QVBoxLayout(Dialog)
            self.verticalLayout.setObjectName("verticalLayout")
            self.label = QtGui.QLabel(Dialog)
            font = QtGui.QFont()
            font.setFamily("Andale Mono")
            font.setPointSize(72)
            self.label.setFont(font)
            self.label.setObjectName("label")
            self.verticalLayout.addWidget(self.label)
            self.label_2 = QtGui.QLabel(Dialog)
            self.label_2.setObjectName("label_2")
            self.verticalLayout.addWidget(self.label_2)
    
            self.retranslateUi(Dialog)
            QtCore.QMetaObject.connectSlotsByName(Dialog)
    
        def retranslateUi(self, Dialog):
            Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog",
    "Dialog", None, QtGui.QApplication.UnicodeUTF8))
            self.label.setText(QtGui.QApplication.translate("Dialog", "less
    UGLY", None, QtGui.QApplication.UnicodeUTF8))
            self.label_2.setText(QtGui.QApplication.translate("Dialog", "Not
    ugly", None, QtGui.QApplication.UnicodeUTF8))
    
    
    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        Dialog = QtGui.QDialog()
        ui = Ui_Dialog()
        ui.setupUi(Dialog)
        Dialog.show()
        sys.exit(app.exec_())
    




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.