1

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?

  • I cannot reproduce this using Qt-4.8.7 and PySide-1.2.4/PyQt-4.11.4 on Linux. Can you provide a [mcve] that demonstrates the problem? What platform are you on, and what specific versions of Qt and PySide/PyQt are you using? – ekhumoro Dec 03 '16 at 18:20
  • I tried to minimize the problem and then I found my problem was elsewhere than I had thought. I found that the application font was not propagated because I have set explicitly the font for the `QPlainTextEdit` at certain line. And then as I found in that case when application font is changed application-wide for certain widget class, it is not propagated to the widgets which have their own font set individually. So I am sorry, this question is misleading and I am going to delete it soon. – HiFile.app - best file manager Dec 03 '16 at 18:57
  • Okay. It might be better to put your comment in an answer, as it is possible that others might benefit from it. – ekhumoro Dec 03 '16 at 19:05
  • Well, I found another problem, which also confused me. If I define my own widget deriving from `QPlainTextEdit` and set the font with `QApplication.setFont(font, "MyWidget")` then it does not work - it does nothing. While `QApplication.setFont(font, "QPlainTextEdit")` works fine even for the derived instances. But this is not what I want, I want to change only the instances of my subclass. (tested in PyQt 5.7) – HiFile.app - best file manager Dec 03 '16 at 19:09
  • Further investigated: `text = MyWidget(); print(text.inherits("MyWidget"))` prints `False`. I think this is the root cause of the problem. – HiFile.app - best file manager Dec 03 '16 at 19:15

1 Answers1

0

I can confirm this behaviour in PyQt-5.7.

It seems to be a bug, because the same problem does not appear in the latest development snapshot (PyQt5_gpl-5.7.1.dev1611251257). So it looks like you will have to use your current work-around until PyQt-5.7.1 is released.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336