summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMilian Wolff <milian.wolff@kdab.com>2019-04-09 13:38:06 +0200
committerMilian Wolff <milian.wolff@kdab.com>2019-04-09 15:46:35 +0000
commit5f2afe18ccb0bbe258d4016ef65218cd3656cac2 (patch)
tree4f20c62ba8bc1fd5d519be1672ebf31ef7dd4f90 /tests
parent6d4a456a28282973d2501e0b16f09cafb316bb0a (diff)
Fix QMetaObject::newInstance on non-QObject meta object
QMetaObject::newInstance returns a QObject, thus it's not possible to create a new instance of a Q_GADGET using this function. Previously, we returned a non-null QObject pointer for such scenarios, which then leads to crashes when one tries to use it. Now, we check whether the meta object inherits QObject's meta object, and error out early otherwise. Change-Id: I7b1fb6c8d48b3e98161894be2f281a491963345e Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp13
1 files changed, 13 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp b/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp
index 9855bec520..cfe1443fd3 100644
--- a/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp
+++ b/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp
@@ -40,6 +40,13 @@ struct MyStruct
int i;
};
+class MyGadget
+{
+ Q_GADGET
+public:
+ Q_INVOKABLE MyGadget() {}
+};
+
namespace MyNamespace {
// Used in tst_QMetaObject::checkScope
class MyClass : public QObject
@@ -1207,6 +1214,12 @@ void tst_QMetaObject::invokeMetaConstructor()
QCOMPARE(obj2->parent(), (QObject*)&obj);
QVERIFY(qobject_cast<NamespaceWithConstructibleClass::ConstructibleClass*>(obj2) != 0);
}
+ // gadget shouldn't return a valid pointer
+ {
+ QCOMPARE(MyGadget::staticMetaObject.constructorCount(), 1);
+ QTest::ignoreMessage(QtWarningMsg, "QMetaObject::newInstance: type MyGadget does not inherit QObject");
+ QVERIFY(!MyGadget::staticMetaObject.newInstance());
+ }
}
void tst_QMetaObject::invokeTypedefTypes()