Modifying the fontsize of a QSpinBox has no effect if a stylesheet was applied to it, while all the other widget classes I tested are resized properly regardless of their stylesheet. QSpinBoxes without a stylesheet are also resized properly. Note that all the widgets' .font().pointSize() are equal so I think this is only a display issue.
A possible workaround would be to save the current stylesheet, set it to None temporarily, resize the font and restore the stylesheet but that sounds horrible and hacky.
I am using Python 3.7.4 and PyQt5 5.12.2.
This is how my MRE looks:
Below is the code which was used:
# -*- coding: utf-8 -*-
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QPushButton, QSpinBox, QMainWindow
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# Setup widgets.
self.widgets = []
for n in range(2):
label = QLabel(parent=self)
label.move(10+150*n, 20)
label.setText("QLabel")
setattr(self, f"label{n+1}", label)
pb = QPushButton(parent=self)
pb.move(10+150*n, 60)
pb.setText("QPushButton")
setattr(self, f"pb{n+1}", pb)
le = QLineEdit(parent=self)
le.move(10+150*n, 100)
le.setText("QLineEdit")
setattr(self, f"le{n+1}", le)
sb = QSpinBox(parent=self)
sb.move(10+150*n, 140)
sb.setSpecialValueText("QSpinBox")
setattr(self, f"sb{n+1}", sb)
for widget in (label, pb, le, sb):
self.widgets.append(widget)
# Take note of the initial window size.
self.initial_size = (self.width(), self.height(), 30)
self.resize(300, 200)
# Paint the right-hand side widgets in red.
for widget in self.widgets[int(len(self.widgets)/2):]:
class_name = widget.__class__.__name__ # QLabel, QPushButton, QSpinBox.
widget.setStyleSheet(class_name + "{background-color: red}")
def resizeEvent(self, event):
"""Adjust the fontsize of all the widgets upon resizing the main window."""
# Calculate a scale factor by comparing current and initial size.
w0, h0, f0 = self.initial_size
w, h = self.width(), self.height()
scale = min(w/w0, h/h0)
# Apply the scale factor to each widget font.
for widget in self.widgets:
font = widget.font()
font.setPointSize(scale * f0)
widget.setFont(font)
# Check that all fontsizes are identical.
assert len(set(widget.font().pointSize() for widget in self.widgets)) == 1
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setAttribute(Qt.AA_DisableHighDpiScaling) # Because it's irrelevant here.
app.setQuitOnLastWindowClosed(True)
window = MainWindow()
window.show()
app.exec_()
