Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Unsolved Scaling a text with multiple font sizes?

    General and Desktop
    qtexted fontsize font
    2
    7
    955
    Loading More Posts
    • 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
      legitnameyo last edited by

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by mrjj

        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 Reply Quote 0
        • L
          legitnameyo last edited by

          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 Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion last edited by mrjj

            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 Reply Quote 0
            • L
              legitnameyo last edited by

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

              mrjj 1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @legitnameyo last edited by

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

                1 Reply Last reply Reply Quote 0
                • L
                  legitnameyo last edited by legitnameyo

                  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 Reply Quote 0
                  • First post
                    Last post