From c938ec01a76bf56f667481e626fbb9252ee3d05d Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Thu, 25 Jun 2009 14:47:57 +0200 Subject: make invokable constructors work with classes in namespace Use the fully qualified classname at relevant places in the moc-generated code. Also, QMetaObject::newInstance() needs to strip the namespace part, since the constructor signatures don't contain the fully qualified name. Task-number: 246064 Reviewed-by: Simon Hausmann --- src/corelib/kernel/qmetaobject.cpp | 8 +++++++- src/tools/moc/generator.cpp | 4 ++-- tests/auto/qmetaobject/tst_qmetaobject.cpp | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index 2d57e0b..71dd5ff 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -218,8 +218,14 @@ QObject *QMetaObject::newInstance(QGenericArgument val0, QGenericArgument val8, QGenericArgument val9) const { + QByteArray constructorName = className(); + { + int idx = constructorName.lastIndexOf(':'); + if (idx != -1) + constructorName.remove(0, idx+1); // remove qualified part + } QVarLengthArray sig; - sig.append(className(), qstrlen(className())); + sig.append(constructorName.constData(), constructorName.length()); sig.append('('); enum { MaximumParamCount = 10 }; diff --git a/src/tools/moc/generator.cpp b/src/tools/moc/generator.cpp index 4814ecc..54305a3 100644 --- a/src/tools/moc/generator.cpp +++ b/src/tools/moc/generator.cpp @@ -918,7 +918,7 @@ void Generator::generateStaticMetacall(const QByteArray &prefix) fprintf(out, " switch (_id) {\n"); for (int ctorindex = 0; ctorindex < cdef->constructorList.count(); ++ctorindex) { fprintf(out, " case %d: { %s *_r = new %s(", ctorindex, - cdef->classname.constData(), cdef->classname.constData()); + cdef->qualified.constData(), cdef->qualified.constData()); const FunctionDef &f = cdef->constructorList.at(ctorindex); int offset = 1; for (int j = 0; j < f.arguments.count(); ++j) { @@ -936,7 +936,7 @@ void Generator::generateStaticMetacall(const QByteArray &prefix) fprintf(out, " }\n"); if (!isQObject) - fprintf(out, " _id = %s::staticMetaObject.superClass()->static_metacall(_c, _id, _a);\n", cdef->classname.constData()); + fprintf(out, " _id = %s::staticMetaObject.superClass()->static_metacall(_c, _id, _a);\n", cdef->qualified.constData()); fprintf(out, " if (_id < 0)\n return _id;\n"); diff --git a/tests/auto/qmetaobject/tst_qmetaobject.cpp b/tests/auto/qmetaobject/tst_qmetaobject.cpp index 95d19e2..dea0ffb 100644 --- a/tests/auto/qmetaobject/tst_qmetaobject.cpp +++ b/tests/auto/qmetaobject/tst_qmetaobject.cpp @@ -605,6 +605,19 @@ void tst_QMetaObject::invokeCustomTypes() QCOMPARE(obj.sum, 3); } +namespace NamespaceWithConstructibleClass +{ + +class ConstructibleClass : public QObject +{ + Q_OBJECT +public: + Q_INVOKABLE ConstructibleClass(QObject *parent = 0) + : QObject(parent) {} +}; + +} + void tst_QMetaObject::invokeMetaConstructor() { const QMetaObject *mo = &QtTestObject::staticMetaObject; @@ -619,6 +632,15 @@ void tst_QMetaObject::invokeMetaConstructor() QCOMPARE(obj2->parent(), (QObject*)&obj); QVERIFY(qobject_cast(obj2) != 0); } + // class in namespace + const QMetaObject *nsmo = &NamespaceWithConstructibleClass::ConstructibleClass::staticMetaObject; + { + QtTestObject obj; + QObject *obj2 = nsmo->newInstance(Q_ARG(QObject*, &obj)); + QVERIFY(obj2 != 0); + QCOMPARE(obj2->parent(), (QObject*)&obj); + QVERIFY(qobject_cast(obj2) != 0); + } } void tst_QMetaObject::normalizedSignature_data() -- cgit v0.12