Screen attached property: expose devicePixelRatio property

Change-Id: I08b22766b3e389b7d27ca4c56729f550b0647a08
Reviewed-by: Jens Bache-Wiig <jensbw@gmail.com>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@digia.com>
Reviewed-by: Morten Johan Sørvig <morten.sorvig@digia.com>
This commit is contained in:
Shawn Rutledge 2014-10-17 14:24:07 +02:00
parent a1b43cb272
commit e2764c7225
5 changed files with 28 additions and 1 deletions

View File

@ -38,7 +38,7 @@
**
****************************************************************************/
import QtQuick 2.1
import QtQuick 2.3
import QtQuick.Window 2.1
Item {
@ -85,6 +85,9 @@ Item {
Text { text: "logical pixel density" }
Text { text: Screen.logicalPixelDensity.toFixed(2) + " dots/mm (" + (Screen.logicalPixelDensity * 25.4).toFixed(2) + " dots/inch)" }
Text { text: "device pixel ratio" }
Text { text: Screen.devicePixelRatio.toFixed(2) }
Text { text: "available virtual desktop" }
Text { text: Screen.desktopAvailableWidth + "x" + Screen.desktopAvailableHeight }

View File

@ -141,6 +141,15 @@ QT_BEGIN_NAMESPACE
The number of physical pixels per millimeter.
*/
/*!
\qmlattachedproperty real Screen::devicePixelRatio
\readonly
\since 5.4
The ratio between physical pixels and device-independent pixels for the screen.
Common values are 1.0 on normal displays and 2.0 on Apple "retina" displays.
*/
/*!
\qmlattachedproperty Qt::ScreenOrientation Screen::primaryOrientation
\readonly
@ -260,6 +269,13 @@ qreal QQuickScreenAttached::pixelDensity() const
return m_screen->physicalDotsPerInch() / 25.4;
}
qreal QQuickScreenAttached::devicePixelRatio() const
{
if (!m_screen)
return 1.0;
return m_screen->devicePixelRatio();
}
Qt::ScreenOrientation QQuickScreenAttached::primaryOrientation() const
{
if (!m_screen)
@ -340,6 +356,8 @@ void QQuickScreenAttached::screenChanged(QScreen *screen)
emit logicalPixelDensityChanged();
if (!oldScreen || screen->physicalDotsPerInch() != oldScreen->physicalDotsPerInch())
emit pixelDensityChanged();
if (!oldScreen || screen->devicePixelRatio() != oldScreen->devicePixelRatio())
emit devicePixelRatioChanged();
connect(screen, SIGNAL(geometryChanged(QRect)),
this, SIGNAL(widthChanged()));

View File

@ -57,6 +57,7 @@ class Q_AUTOTEST_EXPORT QQuickScreenAttached : public QObject
Q_PROPERTY(int desktopAvailableHeight READ desktopAvailableHeight NOTIFY desktopGeometryChanged)
Q_PROPERTY(qreal logicalPixelDensity READ logicalPixelDensity NOTIFY logicalPixelDensityChanged)
Q_PROPERTY(qreal pixelDensity READ pixelDensity NOTIFY pixelDensityChanged)
Q_PROPERTY(qreal devicePixelRatio READ devicePixelRatio NOTIFY devicePixelRatioChanged)
// TODO Qt 6 Rename primaryOrientation to orientation
Q_PROPERTY(Qt::ScreenOrientation primaryOrientation READ primaryOrientation NOTIFY primaryOrientationChanged)
// TODO Qt 6 Remove this orientation -> incomplete device orientation -> better use OrientationSensor
@ -74,6 +75,7 @@ public:
int desktopAvailableHeight() const;
qreal logicalPixelDensity() const;
qreal pixelDensity() const;
qreal devicePixelRatio() const;
Qt::ScreenOrientation primaryOrientation() const;
Qt::ScreenOrientation orientation() const;
Qt::ScreenOrientations orientationUpdateMask() const;
@ -91,6 +93,7 @@ Q_SIGNALS:
void desktopGeometryChanged();
void logicalPixelDensityChanged();
void pixelDensityChanged();
void devicePixelRatioChanged();
void primaryOrientationChanged();
void orientationChanged();
void orientationUpdateMaskChanged();

View File

@ -9,6 +9,7 @@ Item {
property int curOrientation: Window.Screen.orientation
property int priOrientation: Window.Screen.primaryOrientation
property int updateMask: Window.Screen.orientationUpdateMask
property real devicePixelRatio: Window.Screen.devicePixelRatio
Window.Screen.orientationUpdateMask: Qt.LandscapeOrientation | Qt.InvertedLandscapeOrientation
}

View File

@ -63,6 +63,8 @@ void tst_qquickscreen::basicProperties()
QCOMPARE(int(screen->orientation()), root->property("curOrientation").toInt());
QCOMPARE(int(screen->primaryOrientation()), root->property("priOrientation").toInt());
QCOMPARE(int(screen->orientationUpdateMask()), root->property("updateMask").toInt());
QCOMPARE(screen->devicePixelRatio(), root->property("devicePixelRatio").toReal());
QVERIFY(screen->devicePixelRatio() >= 1.0);
}
QTEST_MAIN(tst_qquickscreen)