简体   繁体   中英

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. 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. 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. Tut this could be cumbersome if there are many widgets and are disparate in type as the findChildren function can get a bit hairy. 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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