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. QTextBrowser and application font
Forum Updated to NodeBB v4.3 + New Features

QTextBrowser and application font

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.0k 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.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on last edited by
    #1

    I have a help file viewer which uses QTextBrowser to display HTML pages. These, as well as a global CSS file, are stored in the application's resources. The styles set in the CSS file are used if I set the text browser's searchPaths property so that the CSS file is found.

    Since I am allowing the user to change the global application font at runtime, I would expect that the text browser would use that if there is no other CSS rule pertaining to font-family, etc. However, although all the other GUI elements reflect the user's choice of font, the HTML pages stubbornly use the default system font (in my case, "Ubuntu").

    I have tried all of the "usual suspects" (setting the default stylesheet for the document, setting a style sheet for the widget "body { font-family:'%1'}" and filling in the arg() in the help viewer's constructor, setting a style sheet in the application). Nothing seems to work.

    Is there some kind of CSS "wild card" I could use which would use the application font at runtime? Obviously, I cannot change the CSS rule once the program is running.

    JonBJ 1 Reply Last reply
    0
    • R Robert Hairgrove

      I have a help file viewer which uses QTextBrowser to display HTML pages. These, as well as a global CSS file, are stored in the application's resources. The styles set in the CSS file are used if I set the text browser's searchPaths property so that the CSS file is found.

      Since I am allowing the user to change the global application font at runtime, I would expect that the text browser would use that if there is no other CSS rule pertaining to font-family, etc. However, although all the other GUI elements reflect the user's choice of font, the HTML pages stubbornly use the default system font (in my case, "Ubuntu").

      I have tried all of the "usual suspects" (setting the default stylesheet for the document, setting a style sheet for the widget "body { font-family:'%1'}" and filling in the arg() in the help viewer's constructor, setting a style sheet in the application). Nothing seems to work.

      Is there some kind of CSS "wild card" I could use which would use the application font at runtime? Obviously, I cannot change the CSS rule once the program is running.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Robert-Hairgrove
      This is purely scribbled in haste. I do not fully understand your situation, but does anything in either https://forum.qt.io/topic/44582/qtextbrowser-stylesheet-font-problem or https://stackoverflow.com/questions/14111126/how-to-use-system-font-in-qtextbrowser help you at all?

      R 1 Reply Last reply
      1
      • JonBJ JonB

        @Robert-Hairgrove
        This is purely scribbled in haste. I do not fully understand your situation, but does anything in either https://forum.qt.io/topic/44582/qtextbrowser-stylesheet-font-problem or https://stackoverflow.com/questions/14111126/how-to-use-system-font-in-qtextbrowser help you at all?

        R Offline
        R Offline
        Robert Hairgrove
        wrote on last edited by
        #3

        @JonB Thanks for the two links. The first thread is particularly interesting, although no real solution was offered there yet.

        I will probably end up generating an additional CSS file at runtime and save that as a temporary file or something.

        JonBJ 1 Reply Last reply
        0
        • R Robert Hairgrove

          @JonB Thanks for the two links. The first thread is particularly interesting, although no real solution was offered there yet.

          I will probably end up generating an additional CSS file at runtime and save that as a temporary file or something.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Robert-Hairgrove
          The way I do "dynamic" CSS where I have to is: I put like a %variable% into the CSS file. While I'm reading the file in at start-up to set it as global application stylesheet, I preprocess such a sequence to required substitution. So my style.css might have

          body { font-size: %FONT_SIZE%px; }
          

          or whatever.

          I wasn't sure what you meant by

          Obviously, I cannot change the CSS rule once the program is running.

          R 1 Reply Last reply
          1
          • JonBJ JonB

            @Robert-Hairgrove
            The way I do "dynamic" CSS where I have to is: I put like a %variable% into the CSS file. While I'm reading the file in at start-up to set it as global application stylesheet, I preprocess such a sequence to required substitution. So my style.css might have

            body { font-size: %FONT_SIZE%px; }
            

            or whatever.

            I wasn't sure what you meant by

            Obviously, I cannot change the CSS rule once the program is running.

            R Offline
            R Offline
            Robert Hairgrove
            wrote on last edited by Robert Hairgrove
            #5

            @JonB All of the HTML files (about 80 of them in English, plus an equal amount in German or whatever language is active) have this:

            <link rel="stylesheet" type="text/css" href="sbbl.css">
            <link rel="stylesheet" type="text/css" href="sbbl_font.css">
            

            I generate a new CSS file sbbl_font.css at runtime depending on the user setting. I have to add the path to the searchPaths of the QTextBrowser.

            In the meantime, I discovered that in the German CSS file, I had forgotten to remove the body { font-family:[etc.] } from the CSS file, so I couldn't find the source of the problem until I did a grep -R ... on my project files.

            It is working now ... I put the "dynamic" CSS into a new file generated at runtime, after adding the additional <link> element to all of the HTML files.

            Thanks again!

            JonBJ 1 Reply Last reply
            2
            • R Robert Hairgrove

              @JonB All of the HTML files (about 80 of them in English, plus an equal amount in German or whatever language is active) have this:

              <link rel="stylesheet" type="text/css" href="sbbl.css">
              <link rel="stylesheet" type="text/css" href="sbbl_font.css">
              

              I generate a new CSS file sbbl_font.css at runtime depending on the user setting. I have to add the path to the searchPaths of the QTextBrowser.

              In the meantime, I discovered that in the German CSS file, I had forgotten to remove the body { font-family:[etc.] } from the CSS file, so I couldn't find the source of the problem until I did a grep -R ... on my project files.

              It is working now ... I put the "dynamic" CSS into a new file generated at runtime, after adding the additional <link> element to all of the HTML files.

              Thanks again!

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Robert-Hairgrove
              Ah, I thought you were doing the stylesheet by setting it yourself on the Qt application, I didn't realise you were doing external <link href="...">.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Robert Hairgrove
                wrote on last edited by
                #7

                Now that I removed the font-family rule from the CSS file(s), I discovered that the application font is used after all! So I only need one CSS file which merely avoids specifying any font family at all ... exactly as I had imagined it SHOULD have worked!

                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