aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/qml/qml/qqmlglobal_p.h2
-rw-r--r--tests/auto/qml/qqmlcpputils/tst_qqmlcpputils.cpp20
2 files changed, 21 insertions, 1 deletions
diff --git a/src/qml/qml/qqmlglobal_p.h b/src/qml/qml/qqmlglobal_p.h
index b52e55c61b..c237af6a7c 100644
--- a/src/qml/qml/qqmlglobal_p.h
+++ b/src/qml/qml/qqmlglobal_p.h
@@ -158,7 +158,7 @@ QT_BEGIN_NAMESPACE
template<class T>
T qmlobject_cast(QObject *object)
{
- if (QQmlMetaObject::canConvert(object, &reinterpret_cast<T>(object)->staticMetaObject))
+ if (object && QQmlMetaObject::canConvert(object, &reinterpret_cast<T>(object)->staticMetaObject))
return static_cast<T>(object);
else
return 0;
diff --git a/tests/auto/qml/qqmlcpputils/tst_qqmlcpputils.cpp b/tests/auto/qml/qqmlcpputils/tst_qqmlcpputils.cpp
index 4189f441a2..40c93fe603 100644
--- a/tests/auto/qml/qqmlcpputils/tst_qqmlcpputils.cpp
+++ b/tests/auto/qml/qqmlcpputils/tst_qqmlcpputils.cpp
@@ -51,6 +51,7 @@ public:
private slots:
void fastConnect();
+ void fastCast();
};
class MyObject : public QObject {
@@ -101,6 +102,25 @@ void tst_qqmlcpputils::fastConnect()
}
}
+void tst_qqmlcpputils::fastCast()
+{
+ {
+ QObject *myObj = new MyObject;
+ MyObject *obj = qmlobject_cast<MyObject*>(myObj);
+ QVERIFY(obj);
+ QCOMPARE(obj->metaObject(), myObj->metaObject());
+ obj->slot1();
+ QCOMPARE(obj->slotCount, 1);
+ delete myObj;
+ }
+
+ {
+ QObject *nullObj = 0;
+ QObject *obj = qmlobject_cast<QObject *>(nullObj);
+ QCOMPARE(obj, nullObj); // shouldn't crash/assert.
+ }
+}
+
QTEST_MAIN(tst_qqmlcpputils)
#include "tst_qqmlcpputils.moc"