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. On Android QScreen::physicalSize() initially returns DPI
Forum Updated to NodeBB v4.3 + New Features

On Android QScreen::physicalSize() initially returns DPI

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
3 Posts 2 Posters 520 Views 1 Watching
  • 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.
  • M Offline
    M Offline
    myk321
    wrote on last edited by
    #1

    Using Qt 6.5.1 with Android Pixel Pro 6 running android 13 (May 2023)

    According to Wikipedia: Pixel 6 Pro has a 6.7 in (170 mm) QHD+ 1440p LTPO OLED display at 512 ppi with a 3120 × 1440 pixel resolution and a 19.5:9 aspect ratio.

    According to the Qt 6.5.1 documentation:

    physicalSize : const QSizeF
    This property holds the screen's physical size (in millimeters)
    The physical size represents the actual physical dimensions of the screen's display.
    Depending on what information the underlying system provides the value might not be entirely accurate.
    

    In mainWindow(), I do:

    QScreen *screen = QGuiApplication::primaryScreen();
    QSizeF dimensions = screen->physicalSize();     //Size in mm
    double diagonal = sqrt((dimensions.width() * dimensions.width()) + (dimensions.height() * dimensions.height()));
    qDebug() << QString("Screen diagonal: ") + QString::number(diagonal) + " = " + QString::number(dimensions.width()) + " (width) x " + QString::number(dimensions.height()) + " (height)";
    

    and get:

    Screen diagonal: 3436.28 = 1440 (width) x 3120 (height)
    

    i.e. on Android QScreen::physicalScreen() is initially returning DPI not mm.

    If I then create a dialogue, perform an http request, setup the UI and repeat the above code to get the screen size I get:

    Screen diagonal 2: 170.488 = 71 (width) x 155 (height)
    

    i.e. at some point QScreen, seems to reset from DPI to mm. Checking immediately after the ui setup still gets the DPI result.

    Problem does not present on Linux (Fedora & Ubuntu) and also does not present on MacOs or IOS.

    Obviously Qt is relying on Android for the screen size. Seems odd that Android would not provide a consistent answer.

    Following the suggestion here: https://stackoverflow.com/questions/30980090/qt-qscreenphysicalsize-returns-incorrect-values

    Leads me to this code:

    qreal x = screen->physicalDotsPerInchX();
    qreal y = screen->physicalDotsPerInchY();
    QSize sz = screen->availableSize();
    qreal height = sz.height() / y;
    qreal width = sz.width() / x;
    double diagonalA = sqrt((height * height) + (width * width));
    qDebug() << QString("DPI based screen diagonal: ") + QString::number(diagonalA) + " = " + QString::number(width) + " (width) x " + QString::number(height) + " (height)";
    

    Which gives a fairly inaccurate: DPI based screen diagonal: 135.286 = 56.6929 (width) x 122.835 (height).

    Want to know the screen size to determine how to setup the UI, so not good enough the reset from DPI to mm is only available later and that the DPI based calc results in inaccurate values.

    Not obvious to me how to translate this JNI based suggestion into Qt 6.5.1 [https://forum.qt.io/topic/80293/a-proper-way-to-detect-if-device-is-either-a-cellphone-or-a-tablet/9](link url)

    Any suggestions?

    Paul ColbyP 1 Reply Last reply
    0
    • M myk321

      Using Qt 6.5.1 with Android Pixel Pro 6 running android 13 (May 2023)

      According to Wikipedia: Pixel 6 Pro has a 6.7 in (170 mm) QHD+ 1440p LTPO OLED display at 512 ppi with a 3120 × 1440 pixel resolution and a 19.5:9 aspect ratio.

      According to the Qt 6.5.1 documentation:

      physicalSize : const QSizeF
      This property holds the screen's physical size (in millimeters)
      The physical size represents the actual physical dimensions of the screen's display.
      Depending on what information the underlying system provides the value might not be entirely accurate.
      

      In mainWindow(), I do:

      QScreen *screen = QGuiApplication::primaryScreen();
      QSizeF dimensions = screen->physicalSize();     //Size in mm
      double diagonal = sqrt((dimensions.width() * dimensions.width()) + (dimensions.height() * dimensions.height()));
      qDebug() << QString("Screen diagonal: ") + QString::number(diagonal) + " = " + QString::number(dimensions.width()) + " (width) x " + QString::number(dimensions.height()) + " (height)";
      

      and get:

      Screen diagonal: 3436.28 = 1440 (width) x 3120 (height)
      

      i.e. on Android QScreen::physicalScreen() is initially returning DPI not mm.

      If I then create a dialogue, perform an http request, setup the UI and repeat the above code to get the screen size I get:

      Screen diagonal 2: 170.488 = 71 (width) x 155 (height)
      

      i.e. at some point QScreen, seems to reset from DPI to mm. Checking immediately after the ui setup still gets the DPI result.

      Problem does not present on Linux (Fedora & Ubuntu) and also does not present on MacOs or IOS.

      Obviously Qt is relying on Android for the screen size. Seems odd that Android would not provide a consistent answer.

      Following the suggestion here: https://stackoverflow.com/questions/30980090/qt-qscreenphysicalsize-returns-incorrect-values

      Leads me to this code:

      qreal x = screen->physicalDotsPerInchX();
      qreal y = screen->physicalDotsPerInchY();
      QSize sz = screen->availableSize();
      qreal height = sz.height() / y;
      qreal width = sz.width() / x;
      double diagonalA = sqrt((height * height) + (width * width));
      qDebug() << QString("DPI based screen diagonal: ") + QString::number(diagonalA) + " = " + QString::number(width) + " (width) x " + QString::number(height) + " (height)";
      

      Which gives a fairly inaccurate: DPI based screen diagonal: 135.286 = 56.6929 (width) x 122.835 (height).

      Want to know the screen size to determine how to setup the UI, so not good enough the reset from DPI to mm is only available later and that the DPI based calc results in inaccurate values.

      Not obvious to me how to translate this JNI based suggestion into Qt 6.5.1 [https://forum.qt.io/topic/80293/a-proper-way-to-detect-if-device-is-either-a-cellphone-or-a-tablet/9](link url)

      Any suggestions?

      Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @myk321,

      Using Qt 6.5.1 ...

      Not sure it helps you, but it looks like this was fixed (or at least improved) in Qt 6.5.2 under QTBUG-112742.

      Cheers.

      M 1 Reply Last reply
      2
      • Paul ColbyP Paul Colby

        Hi @myk321,

        Using Qt 6.5.1 ...

        Not sure it helps you, but it looks like this was fixed (or at least improved) in Qt 6.5.2 under QTBUG-112742.

        Cheers.

        M Offline
        M Offline
        myk321
        wrote on last edited by
        #3

        @Paul-Colby thanks Paul - looks good - will wait for the fix!

        1 Reply Last reply
        0

        • Login

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