0

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?

oggmonster
  • 4,672
  • 10
  • 51
  • 73
  • Have you used layouts in your dialogs? –  Jul 22 '13 at 08:41
  • It is complex problem. During customization you should not use any "fixed" values. For example, all fonts sizes should be set in relative values (not in pixels). – Dmitry Sazonov Jul 22 '13 at 08:42
  • Check out this related thread: http://stackoverflow.com/questions/20464814/changing-dpi-scaling-size-of-display-make-qt-applications-font-size-get-rendere/29417620#29417620 – BuvinJ Apr 02 '15 at 16:19

1 Answers1

2

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_())
    
Frodon
  • 3,684
  • 1
  • 16
  • 33
  • Layouts cannot adjust the size of the outer widget, that still has to get set to the maximum size you know you will require – oggmonster Jul 22 '13 at 08:22
  • @oggmonster if you don't manually restrict sizes of widgets (with qss or setMaximumXXX methods) and if you correctly use layouts - than all should be OK. Outer widgets should be also placed in layouts. – Dmitry Sazonov Jul 22 '13 at 08:41
  • If your outer widget has a maximum size, you won't be able to deal with any font size. Either you define a maximum font size, either you release the size constraint of the outer widget. – Frodon Jul 22 '13 at 08:50
  • @Frodon, could you provide small sample with "ugly" font sizes? – Dmitry Sazonov Jul 22 '13 at 14:37