Important: Please read the Qt Code of Conduct -

Scaling a text with multiple font sizes?

  • I got a text where the first part has font size 12 and the last part has font size 24. Is there a way to scale the whole text so that the first part gets font size 24 and the last part gets 48, all in one function?

  • Lifetime Qt Champion

    Text don't really have fonts, so i assume you have some Rich/HTML text with font settings?
    And where do you have this text?

  • Yes, sorry! I've got a QTextEdit with some text in it and part of the text has one font size and another part of the text got another font size. I want to select ALL of the text inside the QTextEdit and make EVERY part of the text twice the size. No matter if some parts of the text have different font sizes or not, it should still scale 2x compared to what it was previously.

  • Lifetime Qt Champion

    Well you would have to loop over all textBlocks
    and grabs its format class and alter the fonts.
    maybe this helps

  • I tried following ideas inside the links you posted but I can only modify blocks of text and not individual characters in the QTextEdit, which is not what I want. I tried to iterate the QTextEdit text as html by doing textEdit->toHtml(); and then iterate it. This only resulted in me getting the individual letters and html code but I can't get the font size of the individual characters I see...

  • Lifetime Qt Champion

    There is also
    So it should be possible.

  • I've tried this but it only scales the first character for some reason, and after I stop scaling the font size returns to normal

        value = 0.3; // random value that increases the font size every time this part of the program is called
        for(int i = -1; i < textEdit->toPlainText().length() - 1; i++) {
            QTextCursor cursor = textEdit->textCursor();
            cursor.setPosition(QTextCursor::Start + i, QTextCursor::MoveAnchor); // for some reason the first letter is start - 1 (start might be 1 and programming languages usually start counting at 0)
            cursor.setPosition(QTextCursor::Start + (i + 1), QTextCursor::KeepAnchor); // next position and keep anchor to highlight
            QTextCharFormat fmt; // resized but don't work
            mergeFormatOnWordOrSelection(fmt); // resized and this works
    void MainWindow::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
       QTextCursor cursor = textEdit->textCursor();
       if (!cursor.hasSelection())
           cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);

Log in to reply