Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are dimensions device independent pixels in QML?

Tags:

qt

qml

I am using Qt 5.6.2

If I am doing this:

Item {
   width: 20
   height: 20
}

Are the values for width and height device independent or exact pixels?

like image 655
Silex Avatar asked Aug 30 '25 17:08

Silex


1 Answers

Usually, it is device-dependent pixels. What is an exact pixel on a monitor where you don't use the maximum resolution?

There are cases where it is not. For example, that is the case if you use scale or where you rely on high DPI scaling. http://doc.qt.io/qt-5/highdpi.html

One of the main reasons for having real values rather than int-values is that it reduces the amount of rounding-up/rounding-down errors, when wanting a size to be a fraction of another size.

See the comment on anchors:

anchors.alignWhenCentered (default true) forces centered anchors to align to a whole pixel; if the item being centered has an odd width or height, the item will be positioned on a whole pixel rather than being placed on a half-pixel. This ensures the item is painted crisply. There are cases where this is not desirable; for example, when rotating the item, aliasing may be apparent as the center is rounded.

Getting device-independent sizes is hard, as the Screen.devicePixelRatio is exactly as stated in the documentation:

The ratio between physical pixels and device-independent pixels for the screen. Common values are 1.0 on normal displays and 2.0 on Apple "retina" displays. This QML property was introduced in Qt 5.4.

So, they don't care whether you have a 4K smartphone or an 800x600 CRT.

The Screen.pixelDensity might be more helpful, but I think it relies on OS/Hardware information. In my experience, it was correct often, but way off sometimes.

like image 71
derM Avatar answered Sep 03 '25 03:09

derM