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. Scaling widget according to screen scaling ratio in OS
QtWS25 Last Chance

Scaling widget according to screen scaling ratio in OS

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 7.4k 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.
  • levi.plL Offline
    levi.plL Offline
    levi.pl
    wrote on last edited by
    #1

    Hi,
    I custom-draw widget using both primitives and text. I defined text size in points and it gets automatically scaled according to OS settings. How can I do the same for primitives so they are scaled proportionally to the text ?

    Currently I'm testing my code on Windows10. I played with idea of figuring out screen scale using QScreen::logicalDotsPerInch() and QScreen::physicalDotsPerInch() but results are not what I expected.

    for 100% scaling Logicalpixel 96 DPI, Physicalpixel 159.856 DPI
    for 150% scaling Logicalpixel 144 DPI, Physicalpixel 159.856 DPI
    for 200% scaling Logicalpixel 192 DPI, Physicalpixel 159.856 DPI

    Is assumption correct that scale always equals to QScreen::logicalDotsPerInch()/96 ?
    Has anyone played with this on other OSes and can share some experience ?

    What do you think about using QFontMetrics ? I can draw primitives using size of reference character QFontMetrics::boundingRect(). It works but I'm not sure how reliable this method is...

    When scale is changed on the fly I noticed that not everything in Qt apps gets re-scaled properly. I assume that this is still WIP in Qt.

    K 1 Reply Last reply
    0
    • levi.plL levi.pl

      Hi,
      I custom-draw widget using both primitives and text. I defined text size in points and it gets automatically scaled according to OS settings. How can I do the same for primitives so they are scaled proportionally to the text ?

      Currently I'm testing my code on Windows10. I played with idea of figuring out screen scale using QScreen::logicalDotsPerInch() and QScreen::physicalDotsPerInch() but results are not what I expected.

      for 100% scaling Logicalpixel 96 DPI, Physicalpixel 159.856 DPI
      for 150% scaling Logicalpixel 144 DPI, Physicalpixel 159.856 DPI
      for 200% scaling Logicalpixel 192 DPI, Physicalpixel 159.856 DPI

      Is assumption correct that scale always equals to QScreen::logicalDotsPerInch()/96 ?
      Has anyone played with this on other OSes and can share some experience ?

      What do you think about using QFontMetrics ? I can draw primitives using size of reference character QFontMetrics::boundingRect(). It works but I'm not sure how reliable this method is...

      When scale is changed on the fly I noticed that not everything in Qt apps gets re-scaled properly. I assume that this is still WIP in Qt.

      K Offline
      K Offline
      KeithS
      wrote on last edited by
      #2

      @levi.pl said:

      Is assumption correct that scale always equals to QScreen::logicalDotsPerInch()/96 ?

      Well that is what I use, based on advice from the Qt folks, for widget scaling on Windows, but I've seen a case where a Linux VM running on a Mac with Retina display broke that assumption.

      Has anyone played with this on other OSes and can share some experience ?

      Mac OSX handles this automatically, except in the case of drawing with OpenGL, in which case scaling by devicePixelRatio() is needed.

      Sorry I can't be of more help.

      levi.plL 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Which version of Qt are you using ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        levi.plL 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          Which version of Qt are you using ?

          levi.plL Offline
          levi.plL Offline
          levi.pl
          wrote on last edited by
          #4

          @SGaist
          Thank you.

          I'm using Qt 5.7.0 64bit compiled with MSVC2015. Build downloaded from qt.io.

          What I noticed is fonts on standard widgets are scaled properly but widgets themselves do not claim more space to fit them.

          This causes fonts to be clipped. I can provide screenshots where you can see the issue on OMenuBar and QGroupbox inside QMainWindow.
          I also noticed that QMessageBox does not grow proportionally to the font size. QCombobox does not scale dropdown list font.

          All this happens when you change scaling factor on the fly. When re-launching application after change everything works fine.

          1 Reply Last reply
          0
          • K KeithS

            @levi.pl said:

            Is assumption correct that scale always equals to QScreen::logicalDotsPerInch()/96 ?

            Well that is what I use, based on advice from the Qt folks, for widget scaling on Windows, but I've seen a case where a Linux VM running on a Mac with Retina display broke that assumption.

            Has anyone played with this on other OSes and can share some experience ?

            Mac OSX handles this automatically, except in the case of drawing with OpenGL, in which case scaling by devicePixelRatio() is needed.

            Sorry I can't be of more help.

            levi.plL Offline
            levi.plL Offline
            levi.pl
            wrote on last edited by levi.pl
            #5

            @KeithS Thank you for confirming "QScreen::logicalDotsPerInch()/96" . I will use it for now. Dynamic scaling on Xorg/Kde does not work so I can't compare.

            Suggestion for Qt folks is to put some guidelines in documentation so people won't have to reinvent the wheel.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              There's documentation on high-dpi support here. Did you already saw it ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              levi.plL 1 Reply Last reply
              0
              • SGaistS SGaist

                There's documentation on high-dpi support here. Did you already saw it ?

                levi.plL Offline
                levi.plL Offline
                levi.pl
                wrote on last edited by levi.pl
                #7

                @SGaist
                I did. Thank you. What I'm missing is examples how to use QScreen in practice. I guess I have to download Qt sources to check how it was done for standard widgets.

                The way I use it now is in custom-drawn-widget class constructor I instantiate QScreen and use calculated scaling ratio to scale all widget's elements and I use signals to redraw widget when scale/dpi/resolution changes. Yet it does not feel right. QScreen should be created for top level widgets and information should be cascaded down to all widgets contained within. That's why I'm seeking advice from professionals :-)

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  AFAIK, you should use QApplication to query the QScreen e.g. the screens method

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  levi.plL 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    AFAIK, you should use QApplication to query the QScreen e.g. the screens method

                    levi.plL Offline
                    levi.plL Offline
                    levi.pl
                    wrote on last edited by levi.pl
                    #9

                    @SGaist
                    That is good suggestion, thanks.

                    I still have couple doubts:

                    1. How to optimally cascade this information down to all widgets.
                    2. Why some elements (like fonts) rendered by Qt are reacting on scale change on-the-fly
                    3. How standard widgets are aware of scaling factor. There must be something already there - this would save me from pain of implementing point 1

                    I went back to "High DPI displays" document I found following suggestions which suggest that document may be obsolete ...

                    *Size windows and dialogs in relation to the screen size.

                    This is opening doors to hardcore GUI implementation ideology but fine. Still problem of proper sizing of non top level widgets is unresolved by this. Moreover if you only resize window you'll get lots of free space...

                    *Replace hard-coded sizes in layouts and drawing code by values calculated from font metrics or screen size.

                    I don't get this one. Drawing code should use screen scale factor and not unreliable font metrics or screen resolution ... If user wants to have interface scaled by two he should get everything twice the normal size.

                    Do you know how to ask someone from Qt team via forum ?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      To contact Qt devs directly, you should bring your points to the interest mailing list. You'll find there Qt developers/maintainers. This forum is more user oriented.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      levi.plL 1 Reply Last reply
                      1
                      • levi.plL Offline
                        levi.plL Offline
                        levi.pl
                        wrote on last edited by levi.pl
                        #11

                        I figured out some things:

                        1. You can get logicaldpi from QWidget inherited member functions: QWidget::logicalDpiX() and QWidget::logicalDpiY(). This way your custom drawn widget can be scaled.

                        2. Fonts are scaled automatically on the fly unless you are using HTML tags. QLabel with plain text will change font size, the one with <font color="red"></font> will not change.

                        3. Standard widgets either do not update hinted sizes or layout manager is not updated an you need to call update geometry manually.

                        4. If you want to scale your widget on the fly then you need to create cascade of signal&slot or setter functions from QScreen to your widget (someone, please,prove me wrong :-) )

                        1 Reply Last reply
                        0
                        • SGaistS SGaist

                          To contact Qt devs directly, you should bring your points to the interest mailing list. You'll find there Qt developers/maintainers. This forum is more user oriented.

                          levi.plL Offline
                          levi.plL Offline
                          levi.pl
                          wrote on last edited by
                          #12

                          @SGaist Thank you for all your help - I will close this thread and open topic with devs - let's check what they have to say :-)

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Don't forget to update the thread once you get more information :)

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0

                            • Login

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