How to change the fontsize for everything inside QTextEdit in PyQt4?

Chris Aung picture Chris Aung · Oct 28, 2014 · Viewed 16.7k times · Source

I have a QTextEdit widget whose contents are populated programmatically using QTextEdit.textCursor.

My plan is to let the user view the populated information in the QTextEdit, edit the text if necessary, and later print to a PDF file using QPrinter.

However, I would like to change the font size of the entire contents of the QTextEdit before allowing the user to edit the text. I just need to set the contents to a single font size; there is no need to accommodate multiple font sizes.

I have tried using QTextEdit.setFontSize(16) both before and after the textCursor operation but it doesn't seem to have any effect.

How do I change the font size of the contents of a QTextEdit widget?

Answer

ekhumoro picture ekhumoro · Oct 28, 2014

Functions like QTextEdit.setFontPointSize work on the current format. If you want to change all the font sizes at once, you need to set the size of the base font, like this:

    font = QtGui.QFont()
    font.setPointSize(16)
    self.editor.setFont(font)

Note that you can also change the relative size of the base-font using the zoomIn and zoomOut slots. The implementation of those slots changes the base-font size in exactly the same way as shown above.