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. Scaling a text with multiple font sizes?
QtWS25 Last Chance

Scaling a text with multiple font sizes?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtextedfontsizefont
7 Posts 2 Posters 1.8k 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.
  • L Offline
    L Offline
    legitnameyo
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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?

      1 Reply Last reply
      0
      • L Offline
        L Offline
        legitnameyo
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          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

          1 Reply Last reply
          0
          • L Offline
            L Offline
            legitnameyo
            wrote on last edited by
            #5

            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...

            mrjjM 1 Reply Last reply
            0
            • L legitnameyo

              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...

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @legitnameyo
              Hi
              There is also https://doc.qt.io/qt-5/qtextcharformat.html
              So it should be possible.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                legitnameyo
                wrote on last edited by legitnameyo
                #7

                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);
                }
                
                1 Reply Last reply
                0

                • Login

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