[Solved] Qt on Android, problem with screen resolution.

  • One of my problems is to write an application usable for all mobiles types.
    I have to use font size= 28 for Samsung S5 mobile and 16 for a little mobile.
    Is there a way to set the sizes, borders, dimension automatically for each device or have I to manage each screen size?
    I have found point size for font but for rectangles, borders How can I do?
    Have you got an example?

  • @mrdebug The QScreen class provides a few usefull functions: logicalDotsPerInch() and physicalDotsPerInch(). physicalDotsPerInch() returns the amount of pixels you have on an inch. You can design your application on a physical size in inch or cm, that will keep your objects resolutionindependent.

    Oh and by the way.. this might be usefull: ScreenSupport

  • Ok, using qml have I to calculate the scale and then apply to all components?
    For example:

    Rectangle {
        anchors.bottomMargin: 4
        anchors.topMargin: 4
    anchors.rightMargin: 4
    anchors.leftMargin: 4
        border.width: 2
        anchors.fill: parent

    should be

    Rectangle {
        anchors.bottomMargin: 4 * scale
        anchors.topMargin: 4 * scale
    anchors.rightMargin: 4 * scale
    anchors.leftMargin: 4 * scale
        border.width: 2 * scale
        anchors.fill: parent

    where scale will be calculated using QScreen?

  • I've wrote a few javascript functions for qml which might work good for you:

    function cm(cm){
                return cm*Screen.pixelDensity*10
            console.warn("Could not calculate 'cm' based on Screen.pixelDensity.")
            return 0
        function mm(mm){
                return mm*Screen.pixelDensity
            console.warn("Could not calculate 'mm' based on Screen.pixelDensity.")
            return 0
        function inch(i){
                return i*Screen.pixelDensity*10*2.54
            console.warn("Could not calculate 'inch' based on Screen.pixelDensity.")
            return 0
        function vw(i){
                return i*(Screen.width/100)
            console.warn("Could not calculate 'vw' based on Screen.width.")
            return 0
        function vh(i){
                return i*(Screen.height/100)
            console.warn("Could not calculate 'vh' based on Screen.height.")
            return 0

    Just add these functions to your root-component/window and you can access them from everywhere.
    Each function returns the pixels from the given system of measurement(which is the functionname). So let's say you want an rectangle which is 2 centimeters high and 4 inches wide:

    Rectangle {
        height: cm(2)
        width: inch(4)

    VH and VW are actually viewport units. VH is 1% of the full height while VW is 1% of the full width.

  • @mrdebug For further information about multiple screen sizes & densities this blog post summarizing our talk at last Qt Dev Days in Berlin might also help: http://v-play.net/2014/11/supporting-multiple-screen-sizes-and-screen-densities-with-qt-and-v-play/

    Alex from V-Play

  • Ok, now I have understand.

Log in to reply