Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How can I set component size according to size & pixel of devices?

How can I set component size according to size & pixel of devices?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
4 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Ibrahim
    wrote on 14 Feb 2018, 14:41 last edited by
    #1

    Hi;
    I have this code:

    Rectangle {
      width: 300
      height: 300
      color: 'red'
    }
    

    But this is not good for all devices, because they are have different pixels & size.
    I use dpi in Android native. How should I set width & height of a component (like Rectangle)? Thanks.

    R 1 Reply Last reply 14 Feb 2018, 14:51
    0
    • I Ibrahim
      14 Feb 2018, 14:41

      Hi;
      I have this code:

      Rectangle {
        width: 300
        height: 300
        color: 'red'
      }
      

      But this is not good for all devices, because they are have different pixels & size.
      I use dpi in Android native. How should I set width & height of a component (like Rectangle)? Thanks.

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 14 Feb 2018, 14:51 last edited by raven-worx
      #2

      @Ibrahim said in How can I set component size according to size & pixel of devices?:

      I use dpi in Android native

      you mean dp no?

      Anyway theoretically you can simulate the dp calculation on any device, using Screen.pixelDensity.
      Then create a method (lets call it dp(real px)) and calculate the value considering the density. Then use this method to calculate the width and height instead setting it "statically" to 300.

      For example android's dp calculation is x * (screenDpi / 160) (whereas x is the value which should appear the same size on all screens)

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      I 1 Reply Last reply 14 Feb 2018, 16:58
      2
      • R raven-worx
        14 Feb 2018, 14:51

        @Ibrahim said in How can I set component size according to size & pixel of devices?:

        I use dpi in Android native

        you mean dp no?

        Anyway theoretically you can simulate the dp calculation on any device, using Screen.pixelDensity.
        Then create a method (lets call it dp(real px)) and calculate the value considering the density. Then use this method to calculate the width and height instead setting it "statically" to 300.

        For example android's dp calculation is x * (screenDpi / 160) (whereas x is the value which should appear the same size on all screens)

        I Offline
        I Offline
        Ibrahim
        wrote on 14 Feb 2018, 16:58 last edited by Ibrahim
        #3

        @raven-worx thanks.
        I think Screen.pixelDensity gives PPI? (https://en.wikipedia.org/wiki/Pixel_density).
        x * screenDpi / 160 formula provides DPI, but how can I calculate DPI with Screen.pixelDensity?
        And what is 160? Is this formula (static 160) running on all platforms (Android & iOS and desktop)?

        R 1 Reply Last reply 14 Feb 2018, 18:03
        0
        • I Ibrahim
          14 Feb 2018, 16:58

          @raven-worx thanks.
          I think Screen.pixelDensity gives PPI? (https://en.wikipedia.org/wiki/Pixel_density).
          x * screenDpi / 160 formula provides DPI, but how can I calculate DPI with Screen.pixelDensity?
          And what is 160? Is this formula (static 160) running on all platforms (Android & iOS and desktop)?

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 14 Feb 2018, 18:03 last edited by
          #4

          @Ibrahim

          Is this formula (static 160) running on all platforms (Android & iOS and desktop)?

          as i said, this is what Android does. In the end it doesn't matter which factor you take - actually thats the definition of a "factor" ;)
          In the end you get what you want...the same size on all screens.
          If you would change the factor, you would also need to adapt the base value your use to compute the sizes.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          2

          1/4

          14 Feb 2018, 14:41

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved