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. QFontMetrics .height and .weight does not change when Windows scale changed.
Forum Updated to NodeBB v4.3 + New Features

QFontMetrics .height and .weight does not change when Windows scale changed.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 862 Views 2 Watching
  • 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.
  • I Offline
    I Offline
    ikuris
    wrote on last edited by ikuris
    #1

    Hello. I am subclassed QAbstractScrollArea and I am doing some drawings in the paintEvent with QPainters drawText function. I am updating the lines between them and their width using QFontMetrics. I have passed the this->font() to QFontMetrics and I am calculating width and height with horizontalAdvance(str) and height() functions and I am calculating these values in the resizeEvent but after changing the scale in the windows these values does not change at all. They are same even though I pass the screen between monitors. How can I correctly calculate the texts width and height that I am drawing.

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      How do you use QFontMetrics? Do you pass the correct paintdevice?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      I 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        How do you use QFontMetrics? Do you pass the correct paintdevice?

        I Offline
        I Offline
        ikuris
        wrote on last edited by ikuris
        #3

        @Christian-Ehrlicher
        I am sorry I didnot understand but I am initalizing the QFontMetrics as fm(this->font()) in the constructor if this is what you are trying to say.

        Christian EhrlicherC 1 Reply Last reply
        0
        • I ikuris

          @Christian-Ehrlicher
          I am sorry I didnot understand but I am initalizing the QFontMetrics as fm(this->font()) in the constructor if this is what you are trying to say.

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @ikuris said in QFontMetrics .height and .weight does not change when Windows scale changed.:

          but I am initalizing the QFontMetrics as fm(this->font())

          And why should the asize of the font change then? How should QFontMetrics know that you now want to know the metrics for another dpi?
          See https://doc.qt.io/qt-6/qfontmetrics.html#QFontMetrics-1

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          A 1 Reply Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            @ikuris said in QFontMetrics .height and .weight does not change when Windows scale changed.:

            but I am initalizing the QFontMetrics as fm(this->font())

            And why should the asize of the font change then? How should QFontMetrics know that you now want to know the metrics for another dpi?
            See https://doc.qt.io/qt-6/qfontmetrics.html#QFontMetrics-1

            A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            @Christian-Ehrlicher said in QFontMetrics .height and .weight does not change when Windows scale changed.:

            @ikuris said in QFontMetrics .height and .weight does not change when Windows scale changed.:

            but I am initalizing the QFontMetrics as fm(this->font())

            And why should the asize of the font change then? How should QFontMetrics know that you now want to know the metrics for another dpi?
            See https://doc.qt.io/qt-6/qfontmetrics.html#QFontMetrics-1

            Just out of curiosity: How would one property initialize QFontMetrics based on a QScreen or a QQuickView?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #6

              @Christian-Ehrlicher already pointed out that you should pass the paint device. QFontMetrics has a constructor for that: https://doc.qt.io/qt-6/qfontmetrics.html#QFontMetrics-1

              Also: QWidget inherits QPaintDevice: https://doc.qt.io/qt-6/qwidget.html . Do you have a QWidget or a class derived from that?

              A 1 Reply Last reply
              0
              • S SimonSchroeder

                @Christian-Ehrlicher already pointed out that you should pass the paint device. QFontMetrics has a constructor for that: https://doc.qt.io/qt-6/qfontmetrics.html#QFontMetrics-1

                Also: QWidget inherits QPaintDevice: https://doc.qt.io/qt-6/qwidget.html . Do you have a QWidget or a class derived from that?

                A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #7

                @SimonSchroeder said in QFontMetrics .height and .weight does not change when Windows scale changed.:

                @Christian-Ehrlicher already pointed out that you should pass the paint device. QFontMetrics has a constructor for that: https://doc.qt.io/qt-6/qfontmetrics.html#QFontMetrics-1

                Also: QWidget inherits QPaintDevice: https://doc.qt.io/qt-6/qwidget.html . Do you have a QWidget or a class derived from that?

                No, that's exactly the point. I need C++ side font metrics calculation for a QML application. Now providing the screen should be enough, since it holds all the necessary DPI information. But there seems to be a corresponding constructor missing for QFontMetrics.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SimonSchroeder
                  wrote on last edited by
                  #8

                  Well, seems like I didn't search enough. I, for once, expected QQuickView to somehow inherit QWidget or at least be a paint device (never used QML so far). Looks like I'm mistaken. Also, before QScreen we got QDesktopWidget which was actually a QWidget. This also got lost.

                  The QFontMetrics always seem to be in relation to the primary screen. So, you need the conversion factor between the primary and your current screen. Our code has something similar to this:

                  qreal pixel_ratio = myApp.getMainWindow()->windowHandle()->screen()->logicalDotsPerInchY()/myApp.primaryScreen()->logicalDotsPerInchY();
                  

                  A lot of things are resized according to the font size. And this pixel ratio is the one we are using to scale the info from QFontMetrics. For example, we might do something like this:

                  QSize iconSize = QSize(QFontMetricsF(QFont()).height() * 2 * myApp.pixelRatio(), QFontMetricsF(QFont()).height() * 2 * myApp.pixelRatio());
                  

                  to rescale our icons (out icons are all SVGs and need to be updated to the new size).

                  One warning: We are still on Qt 5. I hope this translates well to Qt 6.

                  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