Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPlainTextEdit change font
Forum Update on Monday, May 27th 2025

QPlainTextEdit change font

Scheduled Pinned Locked Moved Unsolved General and Desktop
qplaintexteditchange font
4 Posts 2 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joel Bodenmann
    wrote on 5 Jun 2016, 21:48 last edited by
    #1

    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.

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 5 Jun 2016, 22:34 last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply 6 Jun 2016, 05:28
      0
      • S SGaist
        5 Jun 2016, 22:34

        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.

        J Offline
        J Offline
        Joel Bodenmann
        wrote on 6 Jun 2016, 05:28 last edited by
        #3

        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.

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 6 Jun 2016, 21:36 last edited by
          #4

          AFAIK the QTextCursor solution is the simplest.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          2/4

          5 Jun 2016, 22:34

          • Login

          • Login or register to search.
          2 out of 4
          • First post
            2/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved