QMake MOC文件被名称空间混淆 [英] QMake moc files confused by namespaces

查看:212
本文介绍了QMake MOC文件被名称空间混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的文件:

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

但是,当Qt生成_moc文件时,它错误地认为'AreaEditor'位于名称空间'Engine'中,这将导致编译失败.

However, when Qt is generating the _moc file, it incorrectly thinks 'AreaEditor' is in the namespace 'Engine', which then causes the compile to fail.

这是QMake生成的moc文件的示例片段:

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: ;
        }
    }
}

这应该是 :

void AreaEditor::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)

头文件"Engine/World/Area.h"具有一个名为"Area"的类,该类位于名称空间"Engine"中(实际上,它位于两个嵌套名称空间"Engine :: World ::"中).似乎在某种程度上使QMake感到困惑!

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!

如果我删除#include并将其注释掉,那么一切都可以正常编译(除了我必须预先声明"Area",然后只能将其用作类中的指针或引用).

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).

因此,我尝试将#include包装在"MOC_SKIP_BEGIN"中,可以在网上找到看似古老的引用,希望QMake跳过该标头.不,仍然无法编译.

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?

推荐答案

来自文档:

http://qt-project. org/doc/qt-5.0/qtdoc/moc.html#command-line-options

您可以明确地告诉moc不要解析头文件的某些部分. moc定义了预处理程序符号Q_MOC_RUN.任何由

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.

这篇关于QMake MOC文件被名称空间混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆