QPlainTextEdit change font



  • I have a problem with changing the font of a QPlainTextEdit: I am able to change the font if I use QWidget::setFont() before text is actually added to the text edit. However, I am unable to change the font of the text edit if it is already displaying text.
    Is it impossible to change the font of a QPlainTextEdit that already contains text? I'd like to avoid clearing and re-reading the file that it is displaying.


  • Lifetime Qt Champion

    HI,

    Unless I'm mistaken, you need to modify QPlainTextEdit's document and change the font for all the texts where you want that to change.



  • I could use a hand here. It seems to be a simple task but after going over the documentation thrice I still haven't found the proper way of doing this.

    As far as I can tell I am supposed to iterate over all QTextBlocks of the QTextDocument and use QTextCharFormat to change the font. However, I can't figure out how to apply a new QTextFormat to a QTextBlock. All the text block accessor functions appear to return a copy of the QTextBlock. I couldn't find a way of modifying it.
    There also appears to be the possibility to change the font using a QTextCursor that spans the entire content by using QTextCursor::setBlockCharFormat() but I am not sure whether this is the correct solution either.


  • Lifetime Qt Champion

    AFAIK the QTextCursor solution is the simplest.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.