简体   繁体   English

使Qt应用程序使用不同的系统字体大小

[英]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. 我正在开发一个针对Windows和Mac OS X的Qt应用程序。我的应用程序中有很多带文本的对话框。 I created the application on Windows, where I sized the dialogs to fit all of my text neatly. 我在Windows上创建了应用程序,在那里我调整了对话框的大小以适应我的所有文本。 As soon as I compiled on Mac OS X, I realised all of the text doesn't fit properly. 当我在Mac OS X上编译时,我意识到所有文本都不合适。 Furthermore, setting a different system font size in the Windows Control Panel causes all of dialog sizes to be incorrect. 此外,在Windows控制面板中设置不同的系统字体大小会导致所有对话框大小不正确。

How are you supposed to handle different system font sizes in Qt? 你如何处理Qt中不同的系统字体大小?

You don't: you let Qt do the job for you. 你没有:你让Qt为你做这份工作。 Use layouts to arrange your widgets; 使用布局来排列小部件; avoid setting fixed sizes. 避免设置固定大小。 Last point: I recommend to use Qt-Designer to create your interfaces. 最后一点:我建议使用Qt-Designer创建接口。

Edit for Dmitry: here are Python files from 2 ui files (generated with pyuic4) each one with 2 QLabel. 编辑德米特里:这里是2个ui文件(用pyuic4生成)的Python文件,每个文件有2个QLabel。 1 Qlabel font is huge (72), the other is the default one (10). 1 Qlabel字体很大(72),另一个是默认字体(10)。

  1. Min and Max size are constrained: here if max font size is 10, the label is correctly displayed. 最小和最大尺寸受到限制:如果最大字体大小为10,则正确显示标签。

     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. 不再限制大小:可以显示字体大小为72的标签。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM