Important: Please read the Qt Code of Conduct -

Rendering to physical sizes, how to let it work on all platforms?

  • I'm creating an application that should render things to physical sizes, i.e. an intermediate goal would be to render a square that is 4x3 cm on all displays (using both iOS & windows).

    (I understand that its not really possible to do this always correct as the real world size of screens varies)

    the problem I ran into today is that on iOS there is a 2x factor which I can't identify by using generic calls.
    returns 1 on a iPad (incorrect? - iOS 9.3) and 2 on a iPhone (correct? - also iOS 9.3)
    or am i missing something?

    using a QQuickPaintedItem derived class in the paint(QPainter *painter) function:

    auto physicalpixelsperinch = painter->device->physicalDpiX() * QGuiApplication::primaryScreen()->devicePixelRatio();

    is (more or less) correct for windows and a iPhone, but not for an iPad.
    what I am I missing here?

  • Hi
    On Desktop and Android, something like this should work:

    import QtQuick.Window 2.2
    Rectangle {
        property real mm: Screen.pixelDensity
        width: 40*mm
        height: 30*mm

    The problem is that, on Android, depending on the devices, the hardware vendors dont supply reliable firmware, and usually Screen.pixelDensity doesnt give acurate values. On desktop, I usualy get correct values. Not sure abou t IOS, but please check it and let me know :)

  • You could check out the pixelToInches function that is included in the V-Play SDK.

  • @johngod Thank you so much, you're right Screen.pixelDensity would be the complete solution if it was in qml,
    sifting trough the qt source i was able that it was derived from:

    double pixelpermm = QGuiApplication::primaryScreen()->physicalDotsPerInch() / 25.4
    //physicalDotsPerInchX() & physicalDotsPerInchY() also exist

    and it seems to be correct on both my iPhone & iPad quite wel (better than all other ways I found so far)

    @Lorenz thanks, but for the time being I want to stick to Qt as it is a paid sdk as soon as you go commercial