In my application I may have multiple QPlainTextEdit
widgets. I also have a global settings of font for this widget and when I change this font in the global setting, I want the new font to be automatically propagated to all existing QPlainTextEdit
instances in my application. I probably need to use QApplication.setFont(font, "QPlainTextEdit")
but this seems to work for other types of widgets but not for QPlainTextEdit
.
My current workarund feels very hackish. I have overridden QPlainTextEdit
, named that subclass CodeEditWidget
and have this hack:
def event(self, event):
if event.type() == QtCore.QEvent.ApplicationFontChange:
self.setFont(Settings.codeFont) # I need to keep the font in some global place
return super(CodeEditWidget, self).event(event)
Well, it works but I do not like it very much. Of course I would prefer to propagate it automatically. And if that is not possible, I would prefer not having to keep it and pass it with some global Settings. Is that possible? How do I get the font to be set inside this event()
function in a standard way?
UPDATE: I narrowed down the problem to this snippet:
import sys
from PyQt5 import QtGui, QtWidgets
class MyText(QtWidgets.QPlainTextEdit):
pass
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super(MyWidget, self).__init__()
text1 = QtWidgets.QPlainTextEdit()
text1.setPlainText("AAA")
text2 = MyText()
text2.setPlainText("AAA")
button = QtWidgets.QPushButton("Push to change font")
button.clicked.connect(self.onButtonClicked)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(text1)
layout.addWidget(text2)
layout.addWidget(button)
def onButtonClicked(self):
font = QtGui.QFont("Courier", 20)
# QtWidgets.QApplication.setFont(font, "QPlainTextEdit")
QtWidgets.QApplication.setFont(font, "MyText")
app = QtWidgets.QApplication(sys.argv)
mainWindow = MyWidget()
mainWindow.show()
result = app.exec_()
sys.exit(result)
While the commented-out line will change the font of both edit boxes, the line with setFont(font, "MyText")
will do nothing.
This problem seems to exist in PyQt5
. On the other side I tested the same with PySide
and PyQt4
(relpaced QtWidgets
with QtGui
) and it works as expected. So it is either an error in PyQt5
or I am doing something wrong?