Solved QTextBrowser and application font
-
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'ssearchPaths
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 thearg()
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.
-
@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? -
@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.
-
@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 mystyle.css
might havebody { 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.
-
@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 thesearchPaths
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 agrep -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!
-
@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="...">
. -
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!