[英]How do I create different font sizes for widgets but allow them to scale with font dialog in PySide2?
Let's say that I have 3 labels and each one is supposed to have different sized text. 假设我有3个标签,每个标签应有不同大小的文本。 Inspect this example code: 检查以下示例代码:
import sys
from PySide2 import QtCore, QtGui, QtWidgets
def Fonty():
ok, font = QtWidgets.QFontDialog.getFont(tableWidget)
if ok:
app.setFont(font)
tableWidget.resizeColumnsToContents()
tableWidget.resizeRowsToContents()
app = QtWidgets.QApplication(sys.argv)
tableWidget = QtWidgets.QTableWidget()
tableWidget.setColumnCount(4)
tableWidget.setRowCount(1)
for x in range(3):
label = QtWidgets.QLabel(tableWidget)
font = QtGui.QFont()
size = 8 + (x * 2)
font.setPointSize(size)
label.setFont(font)
label.setText("Testing")
tableWidget.setCellWidget(0, x, label)
tableWidget.resizeColumnsToContents()
pushButton = QtWidgets.QPushButton()
pushButton.setText("Fonts")
pushButton.clicked.connect(Fonty)
tableWidget.setCellWidget(0, 3, pushButton)
tableWidget.resizeColumnsToContents()
tableWidget.show()
sys.exit(app.exec_())
The button labeled "Fonts" will open a standard Qt font dialog box. 标有“字体”的按钮将打开一个标准的Qt字体对话框。 When I run this I can see that the headers of the table and the text in the button change size when I change the font size via the dialog box. 运行此命令时,我可以看到当我通过对话框更改字体大小时,表头和按钮中的文本更改了大小。 But the labels that have a fixed font size don't change. 但是具有固定字体大小的标签不会更改。 What if I would like them to change in size as the font size changes? 如果我希望它们随着字体大小的变化而变化怎么办? How do I "scale" the size of the font for these widgets? 如何“缩放”这些小部件的字体大小?
The only solution I've figured out so far is to create a scale factor for each widget and then, when changing font, iterate through all the widgets that need this treatment and scale their fonts individually. 到目前为止,我唯一想到的解决方案是为每个小部件创建一个比例因子,然后在更改字体时,遍历所有需要这种处理的小部件并分别缩放其字体。 This works and isn't bad for a simple GUI. 这行得通,对于简单的GUI来说也不错。 Tut this could be cumbersome if there are many widgets and are disparate in type as the findChildren function can get a bit hairy. 如果有许多小部件并且类型不同,则这可能很麻烦,因为findChildren函数可能会变得有些毛茸茸。 I would love to know if there is a better way to do this. 我很想知道是否有更好的方法可以做到这一点。 Perhaps there is a Qt method that I just haven't found yet? 也许有一种我尚未发现的Qt方法?
import sys
from PySide2 import QtCore, QtGui, QtWidgets
def Fonty():
ok, font = QtWidgets.QFontDialog.getFont(tableWidget)
if ok:
app.setFont(font)
size = font.pointSize()
children = tableWidget.findChildren(QtWidgets.QLabel)
for child in children:
font.setPointSize(size * child.scaleF)
child.setFont(font)
tableWidget.resizeColumnsToContents()
tableWidget.resizeRowsToContents()
app = QtWidgets.QApplication(sys.argv)
fontsize = app.font().pointSize()
tableWidget = QtWidgets.QTableWidget()
tableWidget.setColumnCount(4)
tableWidget.setRowCount(1)
for x in range(3):
label = QtWidgets.QLabel(tableWidget)
font = QtGui.QFont()
size = 8 + (x * 2)
label.scaleF = size/fontsize
font.setPointSize(size)
label.setFont(font)
label.setText("Testing")
tableWidget.setCellWidget(0, x, label)
tableWidget.resizeColumnsToContents()
pushButton = QtWidgets.QPushButton()
pushButton.setText("Fonts")
pushButton.clicked.connect(Fonty)
tableWidget.setCellWidget(0, 3, pushButton)
tableWidget.resizeColumnsToContents()
tableWidget.show()
sys.exit(app.exec_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.