MOC 将命名空间添加到类名中

发布于 2024-08-29 21:46:24 字数 501 浏览 4 评论 0原文

我在编译项目时遇到了这个非常奇怪的问题。 MOC 似乎在被 moc'ed 的类名中添加了一个命名空间,尽管文件/类中没有提到它。

然而,命名空间存在于我使用的库中,但它隐藏在头文件中很远的地方,并且我不在 UI 文件中使用它。这就是 MOC 生成的内容:

const QMetaObject SmpTl::CaptureController::staticMetaObject = {
{ &QObject::staticMetaObject, qt_meta_stringdata_SmpTl__CaptureController,
  qt_meta_data_SmpTl__CaptureController, 0 }};

SmpTl 命名空间在 CaptureController 声明中的任何地方都没有提及,但它出现在 MOC 生成的 .cpp 文件中。

我正在使用带有 QT 集成的 Visual Studio。

I have this very strange problem while compiling the project.
MOC seems to be adding a namespace to the class name being moc'ed, although it's not mentioned anywhere in the file/class.

The namespace, however, exists in a library which I use, but it's hidden far away in the header files and I don't use it in the UI files. This is what MOC generates:

const QMetaObject SmpTl::CaptureController::staticMetaObject = {
{ &QObject::staticMetaObject, qt_meta_stringdata_SmpTl__CaptureController,
  qt_meta_data_SmpTl__CaptureController, 0 }};

The SmpTl namespace is not mentioned anywhere in the declaration of CaptureController, but it appears in the MOC-generated .cpp file.

I'm using Visual Studio with the QT integration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风吹雪碎 2024-09-05 21:47:24

这是与 Q_OBJECT micros 和 MOC 有关的问题。
如果您在另一个包含 Q_OBJECT 的头文件中添加任何包含命名空间的头文件,则可能会遇到此问题。

要解决此问题,您可以禁用标头,包括启用 MOC 时。

#ifndef Q_MOC_RUN
#include "your_header_with_namespace.h"
#endif

您可以将其放置在包含 Q_OBJECT 宏的头文件中,以确保 MOC 跳过包含名称空间的头文件。

下面是一个示例:

#ifndef Q_MOC_RUN
#include "your_header_with_namespace.h"
#endif

class A : public QObject {
    Q_OBJECT

public:
    A() : QObject() {}
};

这应该有助于解决问题,允许 MOC 处理文件,而不会遇到命名空间问题。

This is an issue related to the Q_OBJECT micros and also MOC.
If you add any header file that contains a namespace in another header file that contains Q_OBJECT, you may see this issue.

To fix this, you can disable the header including when MOC is enabled.

#ifndef Q_MOC_RUN
#include "your_header_with_namespace.h"
#endif

You can place this in the header file that contains the Q_OBJECT macro to ensure that MOC skips the namespace-containing header.

Here’s an example:

#ifndef Q_MOC_RUN
#include "your_header_with_namespace.h"
#endif

class A : public QObject {
    Q_OBJECT

public:
    A() : QObject() {}
};

This should help resolve the issue by allowing MOC to process the file without running into problems with the namespaces.

雪若未夕 2024-09-05 21:47:11

SmpTlCaptureController 定义的命名空间,由 MOC 找到。

Q_OBJECT 宏扩展为类定义中 staticMetaObject 变量的声明(以及它扩展的其他内容)。 MOC 文件包含该变量的定义。

如果这不正确,请发布您的 Qt 版本和头文件的精简版本。

SmpTl is the namespace CaptureController is defined in, as it was found by MOC.

The Q_OBJECT macro expands into the declaration of the staticMetaObject-variable inside your class definition (among other things it expands into). The MOC-file contains the definition of that variable.

If this is not correct, please post your Qt version and a stripped down version of your header-file.

森林迷了鹿 2024-09-05 21:46:55

我也遇到了这个问题。我的代码看起来像这样:

namespace foo {
    #ifdef _WIN32
    ...   // This code was fine
    #else
    #error Not Supported
    #endif
}

这让 MOC 误以为命名空间 foo 从未关闭。显然,它不知道 _WIN32 已定义,并且由于我忘记在错误消息周围加上引号而被绊倒。将其更改为:

#error "Not Supported"

解决了我的问题。

I also ran into this problem. I had code that looked like this:

namespace foo {
    #ifdef _WIN32
    ...   // This code was fine
    #else
    #error Not Supported
    #endif
}

This confused MOC into thinking namespace foo never closed. Apparently, it didn't know _WIN32 was defined, and got tripped up by the fact that I forgot to put quotes around the error message. Changing it to:

#error "Not Supported"

fixed my problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文