Nominate our 2022 Qt Champions!

QTextEdit change font of individual paragrahs before rendering

  • I'm having a little trouble with this one small thing. I'm a recent player with Qt, but got years of experience with a different framework in C++.

    I'm using a QTextEdit. I need to change the font attributes of a paragraph just before it is rendered. These attributes are unknown when the text is loaded, and the user can decide arbitrarily to give each paragrah a different "style". Bold/Italic/Underlined on individual words must be preserved, but any other shape of text formatting isn't allowed to be included in the block, these needs to be decided by the application.

    Setting charFormat on the block doesn't work, because setting it after text is loaded does nothing. Furthermore, I'd rather not have formating information included in the text.

    As I understand, I need to set some font just before the text layout engine quicks it, but I don't know where it has to be done.

  • Hi,

    I don't have much experience with rich text editing in Qt, so I hope I don't mislead you.

    According to the "QTextCursor documentation": setting the character format for an already inserted text block is possible. So it seems that you should navigate to the block in question with QTextCursor and use QTextCursor::setBlockCharFormat() to apply a different QTextCharFormat to that text block.

Log in to reply