Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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

    Hi
    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

    Hi
    Well you would have to loop over all textBlocks
    https://doc.qt.io/qt-5/qtextblock.html#details
    and grabs its format class and alter the fonts.
    maybe this helps
    https://stackoverflow.com/questions/27716625/qtextedit-change-font-of-individual-paragraph-block



  • 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

    @legitnameyo
    Hi
    There is also https://doc.qt.io/qt-5/qtextcharformat.html
    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
    
            cursor.charFormat().font().setPointSize(cursor.charFormat().font().pointSize()*(value+1));
            QTextCharFormat fmt; // resized but don't work
            fmt.setFontPointSize(cursor.charFormat().font().pointSize()*(value+1));
    
            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);
       cursor.mergeCharFormat(format);
       textEdit->mergeCurrentCharFormat(format);
    }
    

Log in to reply