PDA

View Full Version : How can I scale all the fonts in a QTextDocument



forgottenduck
20th April 2017, 01:41
So I have a tricky problem that I need an efficient solution to. I need to be able to apply a product-wide zoom to my application. That part isn't too hard, everything gets font sizes from the application style sheet, or asks a helper class for its font, so I can just scale those up based on a zoom factor.

The problem is that I have text fields and tables where the user is able to alter font sizes, and they can apply a larger font to individual words/characters. So is there any simple way that I can maybe change the scaling factor for point size fonts or something?

The solution I can see is scanning through the various QTextDocuments when they are painted and watch for where the font size changes and alter the document. It seems like this would be expensive though.

Any ideas from the community?

d_stranz
20th April 2017, 22:57
Are you using QTextView or QTextBrowser for display? Sounds like QTextView::zoomIn() and QTextView::zoomOut() already do what you want.

Otherwise, can you override the paintEvent() for your view class and apply a scale transform before passing the paintEvent up to the base class?

forgottenduck
21st April 2017, 20:19
We are using classes that derive from QTextEdit for our editors, but it doesn't seem like QTextEdit::zoomIn() is doing what I would expect it to do. The zoom functions work fine for unformatted text, but if I apply a font size to my text then zoom in doesn't appear to actually do anything.

For example: I have the following text string saved for one of the table indexes:


"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:5pt;\">test</span><span style=\" font-size:15pt;\">test</span></p>"

When that html is set to the editor and I then apply zoomIn(5), the point size appears to be unaffected.