QTextBrowser stylesheet font problem
Pt_develop last edited by
I've run into a problem using stylesheets with QTextBrowser. I can set the color (background and font) with a sytlesheet but the using a stylesheet to set other font properties doesn't work. I've tried both
but neither seems to work (I've also tried variations such as px that don't work either).
I've search the documentation and forums but cannot find any restrictions on using stylesheets to set font properties in QTextBrowser. Indeed I've found examples of doing just this for QTextEdit and since QTextBrowser is just a subclass of QTextEdit I presumed using stylesheets to set font properities should work but I can't seem to make it work.
I can set the font size directly using the setFontPointSize() method but for flexibility I would prefer to use stylesheets.
Does anyone know of any QTextBrowser stylesheet restrictions? Perhaps I need to set some flag which I've overlooked. At the moment I'm stumped and would appriciate any help.
BlastDV last edited by
Hi P_Tarr, I’ve been reading around a bit and also tested this on Qt 5.3 and no, it doesn’t work.
This should be the right way:
I’ve also seen that you can only change the font size with the Rich Text Editor , and as you said, with setFontPointSize()
But also reading QTextBrowser’s reference, I found this:
If you want to provide your users with an editable rich text editor, use QTextEdit. If you want a text browser without hypertext navigation use QTextEdit, and use QTextEdit::setReadOnly() to disable editing. If you just need to display a small piece of rich text use QLabel.
While other answers arrive, do you really need to use QTextBrowser?
Pt_develop last edited by
I've been investigating this further and found some interesting behavior. However I need to mention that the behavior has nothing to do with QTextBrowser. Exactly the same behavior occurs with QTextEdit.
In particular my test programs verified the following:
- The Qt stylesheet cascade refers to widgets not rich text editing (yes, I finally found this in the documentation but it wasn't obvious). Roughly this corresponds to only setting the <body> style in an HTML document. HTML element type selectors are not supported.
- QTextEdit and QTextBrowser have, in effect, two stylesheets. The widget stylesheet which is set using the editWidget->setStyleSheet() method and a document default stylesheet which is set using the editWidget->document()->setDefaultStyleSheet() method.
The former is applied to the editWidget itself while the latter is applied to inserted HTML using the editWidget->setHtml() method and the two need not be identical.
- Unlike CSS in a web editor once you've started entering text into an editor resetting the style sheet has little effect with respect to font properties. The editor uses the font properties of the nearest previous character font regardless of the stylesheet in effect. As a result the Widget stylesheet font properties are basically only applied to the first character in the document and thereafter the document character font properties are determined by the previous character.
- One might think (at least I did) that you could use the toHtml() method to save the document HTML and then reset the document using the setHtml() method in order to change the font properties of the document but this doesn't work because the font properties are embedded in the HTML via <span> elements which contain style attributes with the previous font properties!
- One workaround is to directly edit the HTML to remove the <span> elements then when the setHtml() method is used to set the editor's contents the document's default stylesheet will be effective but this can get tedious. One could write a little routine to remove these <span> elements automatically but unfortunately this would also remove legitimate <span> elements.
I still haven't figured out a solution for changing the editor's stylesheet dynamically but now that I've identified the reasons behind the editors behavior I can try to find a solution.