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

2

7

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?

Ypsilanti answered 28/10, 2014 at 2:39 Comment(1)
Is pyqt.sourceforge.net/Docs/PyQt4/qtextedit.html#setFontPointSize works as you want?Unstained
U
12

I found full solution. You should:

  • remember current textCursor
  • call selectAll
  • call setFontPointSize
  • call setTextCursor to clear selection

In C++ it can be done with the following code(it is just example but it solves your problem):

QTextCursor cursor = ui->textEdit->textCursor();
ui->textEdit->selectAll();
ui->textEdit->setFontPointSize(32);
ui->textEdit->setTextCursor( cursor );
Unstained answered 28/10, 2014 at 14:14 Comment(1)
Only selectAll() then setFontPointSize() can take effect.Faradism
J
19

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.

Johnie answered 28/10, 2014 at 17:52 Comment(4)
After setPointSize, zoom have no effect ! Can not use both in the same time?Faradism
@sonichy. It works fine for me. You must be doing something different somehow. Maybe you could try with different font-family? It could be that some fonts aren't scalable.Johnie
how do you know that QTextEdit.setFontPointSize work only on the current format ? No description of this behavior in the doc doc.qt.io/qt-5/qtextedit.html#setFontPointSizePallas
@iMath. The docs say: "Sets the point size of the current format".Johnie
U
12

I found full solution. You should:

  • remember current textCursor
  • call selectAll
  • call setFontPointSize
  • call setTextCursor to clear selection

In C++ it can be done with the following code(it is just example but it solves your problem):

QTextCursor cursor = ui->textEdit->textCursor();
ui->textEdit->selectAll();
ui->textEdit->setFontPointSize(32);
ui->textEdit->setTextCursor( cursor );
Unstained answered 28/10, 2014 at 14:14 Comment(1)
Only selectAll() then setFontPointSize() can take effect.Faradism

© 2022 - 2024 — McMap. All rights reserved.