Add converter for dpi to pixel font size

Add a little function used to convert if needed the size of a font set
in dpi to pixel. Because the conversion from pdi to pixel can't
be exactly identical, the text size in the diagram can be a little
different, in the other side the switch between screens with different
dpi is no more a problem.
This commit is contained in:
joshua 2025-03-12 19:14:45 +01:00
parent 4ca0bbf682
commit ae5e188866
2 changed files with 19 additions and 0 deletions

View File

@ -132,3 +132,21 @@ bool QETUtils::sortBeginIntString(const QString &str_a, const QString &str_b)
return str_a<str_b;
}
}
/**
* @brief QETUtils::pixelSizedFont
* Set the font size to pixelSize instead of pointSize (if needed).
* The font used to draw diagram must be pixel sized instead of point sized (default by Qt)
* then no matter the screen dpi used, the text in diagram keep the same size.
* For more information see @link https://qelectrotech.org/forum/viewtopic.php?pid=6267#p6267 @endlink
* @param font
*/
void QETUtils::pixelSizedFont(QFont &font)
{
if (font.pixelSize() > -1) {
return;
}
auto px = font.pointSizeF()/72 * QFontMetrics{font}.fontDpi();
font.setPixelSize(qRound(px));
}

View File

@ -31,6 +31,7 @@ namespace QETUtils
QString marginsToString(const QMargins &margins);
QMargins marginsFromString(const QString &string);
qreal graphicsHandlerSize(QGraphicsItem *item);
void pixelSizedFont (QFont &font);
bool sortBeginIntString(const QString &str_a, const QString &str_b);