QFontMetrics .height and .weight does not change when Windows scale changed.
-
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.
-
How do you use QFontMetrics? Do you pass the correct paintdevice?
-
How do you use QFontMetrics? Do you pass the correct paintdevice?
@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-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.@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 -
@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@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-1Just out of curiosity: How would one property initialize QFontMetrics based on a QScreen or a QQuickView?
-
@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?
-
@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?
@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.
-
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.