enum
s. They are in the same namespace, but different header files.//C1.h
#include <QObject>
namespace SW
{
Q_NAMESPACE
enum class Enum1 {A, B};
Q_ENUM_NS(Enum1)
}
//C2.h
#include <QObject>
namespace SW
{
Q_NAMESPACE
enum class Enum2 {A, B};
Q_ENUM_NS(Enum2)
}
//main.c
#include <QDebug>
#include <QMetaEnum>
#include "C1.h"
#include "C2.h"
int main(int argc, char *argv[]) {
auto me1 = QMetaEnum::fromType<SW::Enum1>();
qDebug() << "me1 valid:" << me1.isValid();
auto me2 = QMetaEnum::fromType<SW::Enum2>();
qDebug() << "me2 valid:" << me2.isValid();
}
moc_C1.o
and moc_C2.o
defined the staticMetaObject
that result from Q_NAMESPACE
Q_NAMESPACE
. Otherwise moc complains "Error: Namespace declaration lacks Q_NAMESPACE macro."me1 valid: true
me2 valid: true
Q_NAMESPACE
), it compiles but malfunctions at runtime:me1 valid: true
me2 valid: false
Question: is there no way to use Q_NAMESPACE
for a namespace spread over multiple files?
Ways that work around this problem I can think of:
It seems to be a known bug according to their issue tracker QTBUG-68611
We will have to live with this limitation until it is solved.
Alternatively, you could (although I do not advise) have an in-between with a file structure such as this:
// internal/C1.h
#include <QObject>
enum class Enum1 {A, B};
Q_ENUM_NS(Enum1)
//internal/C2.h
#include <QObject>
enum class Enum2 {A, B};
Q_ENUM_NS(Enum1)
// C.h
#include <QObject>
namespace SW
{
Q_NAMESPACE
#include internal/C1.h
#include internal/C2.h
}