Changing Dpi Scaling Size of Display Make Qt Application's Font Size Get Rendered Bigger

Qt: Why DPI decrease on increasing Scaling from OS

I initially suggested using QWindow::devicePixelRatio() in this answer. However, as of Qt 5.12, this does not actually work correctly yet. Qt will avoid fractional scaling ratios due to a bug in Qt's UI scaling implementation. So for 125%, it will still return a 1.0 scaling factor. At 175%, it will return 2.0 instead of 1.75.

So for now, you should fall back to querying the DPI. However, query the logical DPI, not the physical DPI as you are doing now. Replace calls to physicalDpi() functions with calls to logicalDpi() ones. This should give you the standard 96/120/144 DPI reported by Microsoft Windows.

The previous part of the answer below can be used once Qt fixes the bug.



Previous answer, applicable once Qt fixes their scaling bug

Query the scaling ratio directly, don't try to infer it from the DPI. You use QWindow::devicePixelRatio() for this. At 100% scaling, this will return 1.0. At 125% scaling, it will return 1.25. And so on. Since this is a scaling factor, you use it as a multiplier for your sizes.

You should call devicePixelRatio() on the window your widget is currently in. This is because different windows can be on different displays on multi-monitor setups.

The window the widget is in can be obtained with QWidget::windowHandle(). This can return null if the widget is not a window. So you should probably write a small helper function that returns the correct DPR (Device Pixel Ratio) for a widget. It should take a QWidget as argument, and if windowHandle() returns null for the widget, walk up the parent tree, calling windowHandle() on each parent until it finds the first one that doesn't return null. Then return windowHandle()->devicePixelRatio(). This will be the correct DPR to use in that widget.

Can QFontMetrics account for windows dpi scaling?

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;

P.S. The question still needs to be revised.

Font size not changing when DPI is changed

This is the solution I come up with, resize all the controls based on the content they are using. For example if it is a PictureBox control after the DPI is increased only the control itself is resized not the actual image, so I will resize the control based on the size of the Image.

so a simple code will look something like this

 Size newSize = missionPbx.Image.Size;
missionPbx.Size = new Size(newSize.Width + 5, newSize.Height+5);

cheers,

es

How to get sharp UI on high dpi with Qt 5.6?

As Qt documentation says:

Use QT_AUTO_SCREEN_SCALE_FACTOR to enable platform plugin controlled per-screen factors.
QT_SCREEN_SCALE_FACTORS to set per-screen factors.
QT_SCALE_FACTOR to set the application global scale factor.

You can try doing what Qt Creator is doing:

static const char ENV_VAR_QT_DEVICE_PIXEL_RATIO[] = "QT_DEVICE_PIXEL_RATIO";
if (!qEnvironmentVariableIsSet(ENV_VAR_QT_DEVICE_PIXEL_RATIO)
&& !qEnvironmentVariableIsSet("QT_AUTO_SCREEN_SCALE_FACTOR")
&& !qEnvironmentVariableIsSet("QT_SCALE_FACTOR")
&& !qEnvironmentVariableIsSet("QT_SCREEN_SCALE_FACTORS")) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
}

Basically important thing is the last line QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);.



Related Topics



Leave a reply



Submit