Unsolved Zoom in QTextEdit with variable size text (normal text, h2 text, etc.)?
-
I'm using QTextEdit as a somewhat flexible text editor in terms of formatting. I would like that to include some amount of variation in text size (ex: through h2 type of HTML tags). The problem is, when I try to use Zoom functionality alongside this, in a document that contains text of default size and text of h2, for example, the Zoom will work on the h2 text, but not the text of default size. I tried this both with using the built-in ZoomIn() and ZoomOut() functions, and with using something custom to manually set point size for the font of the QTextEdit box, like follows:
const int zoomPerOp = 2; QFont font = ui.textEdit->font(); font.setPointSize(font.pointSize() + zoomPerOp); ui.textEdit->setFont(font);
(With both this code and ZoomIn(), if h2 text is present, its size increases, but other text doesn't.)
I'm struggling to find information on how to account for variation in text size when zooming in QTextEdit, or if it's possible in this context. My impression from reading was that ZoomIn() and ZoomOut() didn't support this due to the relative nature of their adjustments, but that doesn't seem to explain why changing the font size directly would have the same limitation.
I hope that makes sense.
-
Hello. I found a solution. QTextEdit/QTextBrowser only applies font size for the text assigned after call setFontSize function. Then you need to reassign text after changing the size:
class CustomTextBrowser : public QTextBrowser{ Q_OBJECT public: CustomTextBrowser(QWidget *parent = nullptr); protected: QString savedText; void wheelEvent(QWheelEvent *e); public slots: void setText(const QString &text); }; CustomTextBrowser::CustomTextBrowser(QWidget *parent) : QTextBrowser(parent){} void CustomTextBrowser::wheelEvent(QWheelEvent *e){ qreal fontPS = fontPointSize(); if ((e->modifiers() == Qt::ControlModifier) && (e->delta() > 0)) { setFontPointSize(fontPS + 2); } else if ((e->modifiers() == Qt::ControlModifier) && (e->delta() < 0) && (fontPS > 8)) { setFontPointSize(fontPS - 2); } QTextBrowser::setText(savedText); QTextBrowser::wheelEvent(e); } void CustomTextBrowser::setText(const QString &text) { savedText = text; QTextBrowser::setText(text); }