Add QMetaMethod::isValid() function

This function provides a proper way of determining whether a function
returned by QMetaObject::method() is valid. (Checking whether
signature() returns a 0 pointer, which e.g. testlib does, is not an
ideal API -- especially given that signature() will soon be removed
and replaced by a function that returns a QByteArray.)

Change-Id: I644f476b09904925f2042945f5d0ad744482b682
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Kent Hansen 2012-02-17 22:12:05 +01:00 committed by Qt by Nokia
parent 9c1680a7ed
commit 5640b0b443
3 changed files with 25 additions and 0 deletions

View File

@ -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

View File

@ -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;

View File

@ -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"