Important: Please read the Qt Code of Conduct -

Scalability on different screens resolution, wrong DPI

  • Hello everyone,

    I am new to Qt Quick but learning fast :)

    I am currently developing an app that will target Android & iOS.

    I have been facing an issue due to the fact that all Android devices does not share the same screen size nor the same DPI.

    I want my graphical items to have the same size (and by that I mean the REAL size, if I take a ruler I want the measure to be the same) on all of my devices.

    So far, I've established a semi working algorithm that will allow me to achieve that.

    //Calculate the DPI of the device
    property real dpi: Screen.pixelDensity*25.4
    Rectangle {
            width: 4 * dpi  // Should be 4 inches on any screen
            height: 2 * dpi; // Should be 2 inches on any screen
            anchors.centerIn: parent
            color: "red"

    The thing is, that this doesn't work on all of my devices. When I'm looking at the value of the DPI on my different devices, it is not always correct.

    Galaxy S5: 479 (should be 432)
    Nexus 9: 288 (correct DPI)
    Nexus 10: 298 ( real DPI is 300, but close enough)

    I don't have more devices to test with, but I don't understand why the GS5 doesn't give the correct value.

    Can someone help me understand this issue ?

    I thank you in advance.



  • Hi. I get the ratio on the C++ side. Maybe it could give you some better results. Here's how I do it:

    #ifndef Q_OS_ANDROID
    	double myRatio = 1.75;
    	double myRatio = app.screens().at(0)->physicalDotsPerInch()/90;
    QQmlApplicationEngine engine;
    QQmlContext* context = engine.rootContext();
    context->setContextProperty("myratio", QVariant(myRatio));

    On iOS there's not need for scaling. The system does that for you. I'm using 1.75 and 90 because my numbers were all messed up, so it was easier to fix the scaling factor than fixing all widths and heights. You should use whichever number suits you. In case you haven't seen yet, more info here:

  • I have been looking into this myself, with no conclusive information on how to reliably accomplish this. It feels like Qt 5.6 ought to automagically do the scaling for you; I intend to post a new question in this forum asking about this. In the meanwhile, the first part of this video will be of interest to you:

    There is code at 06:24 that extracts the dpi value from the Android framework. Maybe that will work better, I have not tried it myself.