diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2016-09-28 11:00:40 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2016-10-13 17:24:41 +0000 |
commit | 1e3df38bcb61054d53b0853fa2648aae87f1044b (patch) | |
tree | a7579d53b693b5c1449bbed5817328f8fb6fe00e /tests | |
parent | b4cdfc4a12d2b9ebc79fe17a2f1aff67bd940d71 (diff) |
Item: sort out mapFromGlobal() and mapToGlobal()
The commit message of 08327da, and the change log of Qt 5.7.0 promised
that mapFromGlobal() and mapToGlobal() were available in QML. But since
the revision 7 of QQuickItem was not registered, this was not entirely
true.
Due to a little quirk in the QML engine's handling of revisioned
methods, mapFromGlobal() and mapToGlobal() were only accessible via
an identifier or property, but not directly:
// works
MouseArea { id: ma; onClicked: console.log(ma.mapToGlobal(Qt.point(mouse.x, mouse.y))) }
// ReferenceError: mapToGlobal is not defined
MouseArea { onClicked: console.log(mapToGlobal(Qt.point(mouse.x, mouse.y))) }
Furhermore, this is inconsistent with how mapFromItem() and mapToItem()
are exposed to QML. Even though the C++ versions of these methods take
QPointF and QRectF, the QML versions take 2-4 number specifying x, y,
width and height:
object mapFromItem(Item item, real x, real y)
object mapFromItem(Item item, real x, real y, real width, real height)
object mapToItem(Item item, real x, real y)
object mapToItem(Item item, real x, real y, real width, real height)
Therefore the signature of mapFromGlobal() and mapToGlobal() should be:
object mapFromGlobal(real x, real y)
object mapToGlobal(real x, real y)
This change implements the QML versions of these methods using
QQmlV4Function, and adds the missing documentation for the QML API.
NOTE: This is QML-only API.
Change-Id: I2ced4836d274c7d1e644ea29fc25dbdd2045001b
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/quick/qquickitem2/data/mapCoordinates.qml | 10 | ||||
-rw-r--r-- | tests/auto/quick/qquickitem2/tst_qquickitem.cpp | 8 |
2 files changed, 18 insertions, 0 deletions
diff --git a/tests/auto/quick/qquickitem2/data/mapCoordinates.qml b/tests/auto/quick/qquickitem2/data/mapCoordinates.qml index 4874a51ebb..b410b445c5 100644 --- a/tests/auto/quick/qquickitem2/data/mapCoordinates.qml +++ b/tests/auto/quick/qquickitem2/data/mapCoordinates.qml @@ -59,6 +59,16 @@ Item { return Qt.point(pos.x, pos.y) } + function mapAToGlobal(x, y) { + var pos = itemA.mapToGlobal(x, y) + return Qt.point(pos.x, pos.y) + } + + function mapAFromGlobal(x, y) { + var pos = itemA.mapFromGlobal(x, y) + return Qt.point(pos.x, pos.y) + } + function checkMapAToInvalid(x, y) { try { itemA.mapToItem(1122, x, y) diff --git a/tests/auto/quick/qquickitem2/tst_qquickitem.cpp b/tests/auto/quick/qquickitem2/tst_qquickitem.cpp index a087efd6b8..438c7bacf1 100644 --- a/tests/auto/quick/qquickitem2/tst_qquickitem.cpp +++ b/tests/auto/quick/qquickitem2/tst_qquickitem.cpp @@ -2337,6 +2337,14 @@ void tst_QQuickItem::mapCoordinates() Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, x), Q_ARG(QVariant, y))); QCOMPARE(result.value<QPointF>(), qobject_cast<QQuickItem*>(a)->mapFromScene(QPointF(x, y))); + QVERIFY(QMetaObject::invokeMethod(root, "mapAToGlobal", + Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, x), Q_ARG(QVariant, y))); + QCOMPARE(result.value<QPointF>(), qobject_cast<QQuickItem*>(a)->mapToGlobal(QPointF(x, y))); + + QVERIFY(QMetaObject::invokeMethod(root, "mapAFromGlobal", + Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, x), Q_ARG(QVariant, y))); + QCOMPARE(result.value<QPointF>(), qobject_cast<QQuickItem*>(a)->mapFromGlobal(QPointF(x, y))); + QString warning1 = testFileUrl("mapCoordinates.qml").toString() + ":35:5: QML Item: mapToItem() given argument \"1122\" which is neither null nor an Item"; QString warning2 = testFileUrl("mapCoordinates.qml").toString() + ":35:5: QML Item: mapFromItem() given argument \"1122\" which is neither null nor an Item"; |