[Development] qt5 modules in namespaces

Denis Dzyubenko denis at ddenis.info
Fri Dec 30 14:55:55 CET 2011


Hi guys,

With a qt5 module that we are developing, qtjsondb, we stumbled upon
an issue with moc and our classes that are inside a namespace. Let me
illustrate it with a code:

jsondb-global.h:

#if defined(QT_NAMESPACE)
#  define QT_ADDON_JSONDB_BEGIN_NAMESPACE namespace QT_NAMESPACE {
namespace QtAddOn { namespace JsonDb {
#  define QT_ADDON_JSONDB_END_NAMESPACE } } }
#  define QT_ADDON_JSONDB_USE_NAMESPACE using namespace
QT_NAMESPACE::QtAddOn::JsonDb;
#else
#  define QT_ADDON_JSONDB_BEGIN_NAMESPACE namespace QtAddOn { namespace JsonDb {
#  define QT_ADDON_JSONDB_END_NAMESPACE } }
#  define QT_ADDON_JSONDB_USE_NAMESPACE using namespace QtAddOn::JsonDb;
#endif

jsondb-map-reduce.h

QT_ADDON_JSONDB_BEGIN_NAMESPACE
class Foo : public QObject {
Q_OBJECT
};
QT_ADDON_JSONDB_END_NAMESPACE


and here lies the problem - the macro QT_ADDON_JSONDB_BEGIN_NAMESPACE
expands to a namespace definition, but moc doesn't know about that, so
the generated moc_jsondb-map-reduce.cpp" expects to find class Foo in
a global namespace:

moc_jsondb-map-reduce.cpp:

QT_BEGIN_MOC_NAMESPACE
void Foo::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int
_id, void **_a)
...


notice the QT_BEGIN_MOC_NAMESPACE - that is a trick for the case when
Qt itself is compiled in a namespace - it is defined in qglobal.h to
"using namespace QT_NAMESPACE"

In qt5 modules we have exactly the same problem as in namespaced-Qt
itself. Hence my suggestion to solve it in the same way - I would like
to be able to re-use that QT_BEGIN_MOC_NAMESPACE for qt5 modules, to
be able to redefine it to use both Qt namespace and my modules'
namespace.

Here is the patch for qtbase: http://codereview.qt-project.org/11892

And here is the patch for qtjsondb: http://codereview.qt-project.org/11894
(see http://codereview.qt-project.org/#patch,sidebyside,11894,1,src/daemon/daemon.pri
for most interesting part :)

Cheers and Happy New Year!
Denis.



More information about the Development mailing list