简体   繁体   中英

Can QFontMetrics account for windows dpi scaling?

I was under the impression that if you do this in your application

QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication* app = new QApplication(temp, NULL);

then Fonts gets automatically scaled up on high resolution display. Same if you explicitly increase font scaling in Windows 10 (Settings-->System->Custom Scaling).

However, when running the following code with 100% and then 200% scaling in Windows 10, it does not return doubled size.

QFont font = QFont("arial", 10);
QFontMetrics fm(font);
int width = fm.width("abcdefgABCDEFG");

Strangely there is only 1 pixel difference.

100% --> width = 108 pixels
200% --> width = 109 pixels

Why is that? Can I get QFontMetrics to account for Windows scaling? Or do I need to use Logical / Physical DPI to deduce that font size must be increased by a factor 2?

Thanks

For the proper scaling of custom-drawn items use QScreen::physicalDotPerInch property to realize the scaling coefficient to apply to actual drawings:

qreal myScale = pScreen->physicalDotPerInch() / constStandardPerInch;

PS The question still needs to be revised.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM