Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. How to control the font size in QWebEngineView?

How to control the font size in QWebEngineView?

Scheduled Pinned Locked Moved Solved QtWebEngine
9 Posts 3 Posters 6.1k 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.
  • R Offline
    R Offline
    roni219
    wrote on 18 Feb 2017, 03:08 last edited by
    #1

    Hello,

    I would like to let the user increase/decrease the font size in the text displayed by QWebEngineView. What is the best way to do it?

    I tried

    QFont f = browser->font(); // browser is a pointer to a QWebEngineView widget
    int p = f.pointSize();
    f.setPointSize(p * 2);     // double the font size
    browser->setFont(f);
    

    and it had no effect.

    Besides, the application uses system default fonts, and this approach may require checking of pointSize(), pixelSize(), and pointSizeF().

    Thank you in advance,

    R.

    M 1 Reply Last reply 18 Feb 2017, 07:38
    0
    • R roni219
      18 Feb 2017, 03:08

      Hello,

      I would like to let the user increase/decrease the font size in the text displayed by QWebEngineView. What is the best way to do it?

      I tried

      QFont f = browser->font(); // browser is a pointer to a QWebEngineView widget
      int p = f.pointSize();
      f.setPointSize(p * 2);     // double the font size
      browser->setFont(f);
      

      and it had no effect.

      Besides, the application uses system default fonts, and this approach may require checking of pointSize(), pixelSize(), and pointSizeF().

      Thank you in advance,

      R.

      M Offline
      M Offline
      matthew.kuiash
      wrote on 18 Feb 2017, 07:38 last edited by matthew.kuiash
      #2

      @roni219 The "font" you have accessed there is the font for the widget (inheritance is QObject->QWidget->QWebEngineView). The fonts rendered in the web page are those for the HTML within not the widget.

      Try using QWebEngineView::setZoomFactor(2.0) to double the size. I suspect this will scale images too. I'm not sure that scaling of images and text is separable.

      The legendary cellist Pablo Casals was asked why he continued to practice at age 90. "Because I think I'm making progress," he replied.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        roni219
        wrote on 19 Feb 2017, 21:51 last edited by
        #3

        Thank you very much, @matthew-kuiash ! The method worked. Indeed, as you predicted, it zooms the images as well, but for the interactive zoom feature it is the right behaviour.

        I will not mark the topic "solved" for a few more days, in case somebody suggests the font control as well. The default fonts on Linux and Windows 10 are very different. The Windows font is very small, it would be nice to set the default to larger font.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mpergand
          wrote on 20 Feb 2017, 23:44 last edited by mpergand
          #4

          You can fixe a minimum font size:

          QWebEngineSettings *defaultSettings = QWebEngineSettings::globalSettings();
          defaultSettings->setFontSize(QWebEngineSettings::MinimumFontSize,16);
          

          For the default font, you can set any font size you like as well:

          QFontDatabase fontDataBase;
          QWebEngineSettings *defaultSettings = QWebEngineSettings::globalSettings();
          QFont standardFont=fontDataBase.font("Arial","",12);
          defaultSettings->setFontFamily(QWebEngineSettings::StandardFont, standardFont.family());
          

          [edit] i'm suspicious about the second solution i provide, as it's set the font familly not the size i guess ...

          R 1 Reply Last reply 21 Feb 2017, 19:19
          0
          • M mpergand
            20 Feb 2017, 23:44

            You can fixe a minimum font size:

            QWebEngineSettings *defaultSettings = QWebEngineSettings::globalSettings();
            defaultSettings->setFontSize(QWebEngineSettings::MinimumFontSize,16);
            

            For the default font, you can set any font size you like as well:

            QFontDatabase fontDataBase;
            QWebEngineSettings *defaultSettings = QWebEngineSettings::globalSettings();
            QFont standardFont=fontDataBase.font("Arial","",12);
            defaultSettings->setFontFamily(QWebEngineSettings::StandardFont, standardFont.family());
            

            [edit] i'm suspicious about the second solution i provide, as it's set the font familly not the size i guess ...

            R Offline
            R Offline
            roni219
            wrote on 21 Feb 2017, 19:19 last edited by
            #5

            Thank you very much, @mpergand! The "minimum font size" method worked!

            I tried it the way you suggested, and also with QWebEngineView::settings(). Either way it worked.

            Curiously, the same font size settings produce different results across the platforms. I checked my initial QWebEngineView font sizes, and they were the same on Ubuntu 14.04 and on Windows 10:

            MinimumFontSize = 0
            MinimumLogicalFontSize = 6
            DefaultFontSize = 16
            DefaultFixedFontSize = 13

            Yet the text is visibly smaller on Windows. Both screenshots below are taken after MinimumFontSize is set to 16:

            Linux:
            alt text
            Windows:
            alt text
            Perhaps the minimum size must be larger for Windows version of the application.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on 21 Feb 2017, 22:39 last edited by
              #6

              Your screenshots doesn't show.

              Unfortunatly, depending the font family the display size could be different even if the font size is the same.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                roni219
                wrote on 21 Feb 2017, 22:49 last edited by
                #7

                @mpergand Oops, sorry, hope these links work:

                Linux:
                https://drive.google.com/open?id=0B3VIzaAFexv9dGNQOTExQWxvWjA

                Windows:
                https://drive.google.com/open?id=0B3VIzaAFexv9bmpkY1BuTWNVOW8

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mpergand
                  wrote on 22 Feb 2017, 09:44 last edited by
                  #8

                  I think is normal.

                  What you can do, is to register the zoomFactor that the user set in the QSettings of your app.
                  When you open a webView, you set back the zoomFactor value from the QSettings.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    roni219
                    wrote on 22 Feb 2017, 18:36 last edited by
                    #9

                    Thank you, @mpergand , QSettings is a very good idea, I will do as you suggested.

                    1 Reply Last reply
                    0

                    5/9

                    21 Feb 2017, 19:19

                    • Login

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