Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 742
Chris Aung Avatar asked Oct 28 '14 02:10

Chris Aung


People also ask

How do I change the default font in Qt?

Simply use the setFont() method on the QApplication or QWidget : QFont font("Courier New"); font. setStyleHint(QFont::Monospace); QApplication::setFont(font);

How do I change font size in labels?

To change the font size in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property font-size. HTML5 do not support the <font> tag, so the CSS style is used to add font size.

How do I change the font in PYQT?

use setFont() : it sets the default font for the target; you can get the current default font using something. font() , then use font. setPointSize() (or setPointSizeF() for float values, if the font allows it) and then call setFont(font) on the target.

How do I change font size in Qt Designer?

Select Fonts & Colors tab. Under the Font heading after where it says Family: select Source Code Pro from the dropdown menu as marked by the mouse cursor in the below screenshot. After where it says Size: select 10 from the dropdown menu. Font size 10 is the best font size in my Qt Creator.


2 Answers

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.

like image 141
ekhumoro Avatar answered Sep 21 '22 00:09

ekhumoro


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 );
like image 31
Kosovan Avatar answered Sep 20 '22 00:09

Kosovan