MOC 将命名空间添加到类名中
我在编译项目时遇到了这个非常奇怪的问题。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是与 Q_OBJECT micros 和 MOC 有关的问题。
如果您在另一个包含 Q_OBJECT 的头文件中添加任何包含命名空间的头文件,则可能会遇到此问题。
要解决此问题,您可以禁用标头,包括启用 MOC 时。
您可以将其放置在包含 Q_OBJECT 宏的头文件中,以确保 MOC 跳过包含名称空间的头文件。
下面是一个示例:
这应该有助于解决问题,允许 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.
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:
This should help resolve the issue by allowing MOC to process the file without running into problems with the namespaces.
SmpTl
是CaptureController
定义的命名空间,由 MOC 找到。Q_OBJECT
宏扩展为类定义中staticMetaObject
变量的声明(以及它扩展的其他内容)。 MOC 文件包含该变量的定义。如果这不正确,请发布您的 Qt 版本和头文件的精简版本。
SmpTl
is the namespaceCaptureController
is defined in, as it was found by MOC.The
Q_OBJECT
macro expands into the declaration of thestaticMetaObject
-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.
我也遇到了这个问题。我的代码看起来像这样:
这让 MOC 误以为命名空间
foo
从未关闭。显然,它不知道_WIN32
已定义,并且由于我忘记在错误消息周围加上引号而被绊倒。将其更改为:解决了我的问题。
I also ran into this problem. I had code that looked like this:
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:fixed my problem.