diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index 09d6713a43..03fc90b620 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -1248,6 +1248,14 @@ bool QMetaObject::invokeMethod(QObject *obj, \value Scriptable */ +/*! + \fn bool QMetaMethod::isValid() const + \since 5.0 + + Returns true if this method is valid (can be introspected and + invoked), otherwise returns false. +*/ + /*! \fn const QMetaObject *QMetaMethod::enclosingMetaObject() const \internal diff --git a/src/corelib/kernel/qmetaobject.h b/src/corelib/kernel/qmetaobject.h index 650369164c..9e51af7556 100644 --- a/src/corelib/kernel/qmetaobject.h +++ b/src/corelib/kernel/qmetaobject.h @@ -134,6 +134,8 @@ public: val0, val1, val2, val3, val4, val5, val6, val7, val8, val9); } + inline bool isValid() const { return mobj != 0; } + private: const QMetaObject *mobj; uint handle; diff --git a/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp b/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp index cb34d411fd..1651d00738 100644 --- a/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp +++ b/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp @@ -52,6 +52,8 @@ class tst_QMetaMethod : public QObject private slots: void method_data(); void method(); + + void invalidMethod(); }; struct CustomType { }; @@ -597,6 +599,7 @@ void tst_QMetaMethod::method() QVERIFY(index != -1); QMetaMethod method = (methodType == QMetaMethod::Constructor) ? mo->constructor(index) : mo->method(index); + QVERIFY(method.isValid()); QCOMPARE(method.methodType(), methodType); QCOMPARE(method.access(), access); @@ -611,5 +614,17 @@ void tst_QMetaMethod::method() QCOMPARE(method.parameterNames(), parameterNames); } +void tst_QMetaMethod::invalidMethod() +{ + QMetaMethod method; + QVERIFY(!method.isValid()); + + QMetaMethod method2 = staticMetaObject.method(staticMetaObject.methodCount()); + QVERIFY(!method2.isValid()); + + QMetaMethod method3 = staticMetaObject.method(-1); + QVERIFY(!method3.isValid()); +} + QTEST_MAIN(tst_QMetaMethod) #include "tst_qmetamethod.moc"