On Android QScreen::physicalSize() initially returns DPI
-
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?
-
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.
-
@Paul-Colby thanks Paul - looks good - will wait for the fix!