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. [SOLVED] Forcing font in QTextBrowser without StyleSheet or HTML
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Forcing font in QTextBrowser without StyleSheet or HTML

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.2k Views 1 Watching
  • 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.
  • Q Offline
    Q Offline
    qtsms
    wrote on last edited by
    #1

    What I'm trying to do is this:

    1. Clone user-editable QTextBrowser's content into read-only QTextBrowser.
    2. Give the user possibility to change font of read-only browser by using QFontDialog. I'm storing the font as string in database.
      I'm trying to do this like that:

    @
    qTextBrowser_ro->setHtml(qTextBrowser_rw->toHtml());
    qFont->fromString(qString);
    qTextBrowser_2->setFont(*qFont);
    @

    It's not working. I know two ways of forcing font inside QTextBrowser: adding HTML tags or setting StyleSheet. I don't want to do that because I don't really know how to easly convert string returned by QFont::toString() to string accepted by QWidget::setStyleSheet() and I don't want to change the content of QTextBrowser itself (by HTML approach) as font can be changed many times...

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qtsms
      wrote on last edited by
      #2

      Solution:
      @
      QTextCursor cursor(browser->textCursor());
      cursor.beginEditBlock();
      cursor.movePosition(QTextCursor::Start);
      cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
      cursor.endEditBlock();
      QTextCharFormat format;
      format.setFont(*qFont);
      cursor.setCharFormat(format);
      @

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Out of curiosity, why is qFont a pointer ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qtsms
          wrote on last edited by
          #4

          bq. Out of curiosity, why is qFont a pointer ?

          Oh, but it doesn't matter. It shouldn't be, there is no need for that.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tiho_d
            wrote on last edited by
            #5

            Hi,
            another out of curiosity, why is cursor.beginEditBlock()/cursor.endEditBlock() needed.

            I believe you can do without them. These calls must be placed around the actual editing call(cursor.setCharFormat(format);) to make any difference. If you do not care about Undo of the font change, you can do without them completely.

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              qtsms
              wrote on last edited by
              #6

              Haha, I'm not "Certified Specialist" like you two, guys. I just solved my problem and it's working so I've posted this solution because I've found that a lot of others had the same problem and it wasn't really solvable through Google.

              I thought startEditBlock/endEditBlock works differently (I won't describe how as it's not true anyway - I just didn't read the docs careful enough). I needed this quick so I didn't really care much about being accurate or efficient, just wanted this to work and shared as soon as it worked. :)

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tiho_d
                wrote on last edited by
                #7

                No, hard feelings. I just wanted to optimize your finding a bit further. :-)

                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