Hi all I have problem with my QML code. I made mistake and I went to put certain size to elements and now I have problem when putting app on other devices. I will paste you my
Changing the device, you are actually changing the screen pixel density. If you have a device with low quality display it will have lower number of pixels per inch or pixels per centimeter than a device with high quality display.
Understanding this we can easily devise a method to scale our content according to the pixel density. For example in my case I have a laptop on which I use Qt creator. It has pixel density of 4. But my android phone is of high quality which has density of 16(4 times more than my Laptop). It means if an item has a width 'X' and height 'Y' when displaying on my Laptop, on my phone it will appear with a width 'X'/4 and height 'Y'/4 . Thus I have to scale height and width by 4.
Now, How to implement this? In QML we have a property "pixelDensity" under object Screen which will give you the pixel density of the screen where you are running your application. Dividing this by the screen density of screen where you tested your application will give you the scale factor. So now you don't need to worry about other devices you have just find the pixel density of screen you are currently working on.
Following code works perfectly for me.
property int default_pix_density: 4 //pixel density of my current screen
property int scale_factor: Screen.pixelDensity/default_pix_density
Rectangle
{
width: 50*scale_factor
height: 20*scale_factor
}
It's unfortunately tricky to get perfect, as it's likely that as the screen size shrinks you might actually want the buttons to be bigger and to drop content from the screen to ensure the user can access and read everything ok.
But the general approach is actually to set a scale factor in the C++ side:
ctxt->setContextProperty("scale", /* put calculated scale factor here */);
And then on the QML side, use that everyone to scale all the objects:
Rectangle {
id:window
width: 602 * scale
height: 1000 * scale
That way you can adjust the scale variable to change everything's size. Having said that, many people end up with different QML files depending on the platform size though.
I recommend you read the Scalability page in the current Qt (4.8) documentation: it is on exactly this topic.
It recommends these techniques (I'm quoting the page here), and then supplies much more detail.
Create separate top-level layout definitions for each form factor.
Keep the layouts small and let components scale relative to their immediate parent.
Define device independent measurements, such as dp (device independent pixels), and use these to scale components and for layout measurement.
Define layouts in a proportional way using the built-in layout features of QML.
Update 2014-11-18 This and video article looks very useful indeed: Supporting Multiple Screen Sizes & Screen Densities with Qt and V-Play
Update 2017-01-24 There is an updated Qt 5.8 version of the Scalability page mentioned above.