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. Zoom in QTextEdit with variable size text (normal text, h2 text, etc.)?
Forum Updated to NodeBB v4.3 + New Features

Zoom in QTextEdit with variable size text (normal text, h2 text, etc.)?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.2k Views
  • 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.
  • T Offline
    T Offline
    Trapezoid_Dreams
    wrote on 18 Sept 2019, 07:41 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sacha_D
      wrote on 5 Dec 2019, 20:56 last edited by
      #2

      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);
      }
      
      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved