问题
I have a file that looks like:
#ifndef ENGINE_PLATFORM_AREAEDITOR_H
#define ENGINE_PLATFORM_AREAEDITOR_H
#include <QWidget>
#include "../Widgets/QtSfmlWidget.h"
namespace Engine { namespace World { class Area; } }
//This tell's Qt's qmake to ignore the code between MOC_SKIP_BEGIN and MOC_SKIP_END
// MOC_SKIP_BEGIN
#include "Engine/World/Area.h"
// MOC_SKIP_END
namespace Ui {
class AreaEditor;
}
class AreaEditor : public QWidget
{
Q_OBJECT
public:
Engine::World::Area area;
public:
explicit AreaEditor(QWidget *parent = 0);
~AreaEditor();
//...stuff....
private slots:
void on_markerTextEdit_textChanged();
void onDrawAreaScreen(sf::RenderTarget &renderTarget);
void onDrawAreaResized(const WindowSize &windowSize);
private:
Ui::AreaEditor *ui;
//...stuff....
};
#endif //ENGINE_PLATFORM_AREAEDITOR_H
However, when Qt is generating the _moc file, it incorrectly thinks 'AreaEditor' is in the namespace 'Engine', which then causes the compile to fail.
Here's an example snippet of the moc file that QMake is generating:
______/---------<-< Wrong
V V
void Engine::AreaEditor::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
if (_c == QMetaObject::InvokeMetaMethod) {
AreaEditor *_t = static_cast<AreaEditor *>(_o);
switch (_id) {
case 0: _t->on_markerTextEdit_textChanged(); break;
case 1: _t->onDrawAreaScreen((*reinterpret_cast< sf::RenderTarget(*)>(_a[1]))); break;
case 2: _t->onDrawAreaResized((*reinterpret_cast< const WindowSize(*)>(_a[1]))); break;
default: ;
}
}
}
Here's what it should be:
void AreaEditor::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
The header file "Engine/World/Area.h" has a class called 'Area' that is in a namespace 'Engine' (actually, it's in 'Engine::World::", two nested namespaces). It seems like this is confusing QMake in some way!
If I remove the #include, commenting it out, everything compiles fine (except I have to pre-declare 'Area', and then can only use it as a pointer or a reference in the class).
So I tried wrapping the #include in a "MOC_SKIP_BEGIN", which I can find seemingly archaic references to online, hoping QMake will skip that header. Nope, still fails to compile.
Is there a way I can get this to compile while still being able to include the header I want to include?
回答1:
From the documentation:
http://qt-project.org/doc/qt-5.0/qtdoc/moc.html#command-line-options
You can explicitly tell the moc not to parse parts of a header file. moc defines the preprocessor symbol Q_MOC_RUN. Any code surrounded by
#ifndef Q_MOC_RUN
...
#endif
is skipped by the moc.
来源:https://stackoverflow.com/questions/18626146/qmake-moc-files-confused-by-namespaces