oggmonster
oggmonster

Reputation: 4872

Making Qt Application that work with different System Font Sizes

I am working on a Qt application targeting Windows and Mac OS X. I have lots of dialogs with text in my application. I created the application on Windows, where I sized the dialogs to fit all of my text neatly. As soon as I compiled on Mac OS X, I realised all of the text doesn't fit properly. Furthermore, setting a different system font size in the Windows Control Panel causes all of dialog sizes to be incorrect.

How are you supposed to handle different system font sizes in Qt?

Upvotes: 0

Views: 3402

Answers (1)

Frodon
Frodon

Reputation: 3775

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_())
    

Upvotes: 2

Related Questions