C++ Q_OBJECT 宏有什么作用?为什么所有 Qt 对象都需要这个宏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1368584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the Q_OBJECT macro do? Why do all Qt objects need this macro?
提问by Trevor Boyd Smith
I just started using Qt and noticed that all the example class definitions have the macro Q_OBJECT
as the first line. What is the purpose of this preprocessor macro?
我刚开始使用 Qt 并注意到所有示例类定义都将宏Q_OBJECT
作为第一行。这个预处理器宏的目的是什么?
回答by Stu Mackellar
From the Qt documentation:
从Qt 文档:
The Meta-Object Compiler, moc, is the program that handles Qt's C++ extensions.
The moc tool reads a C++ header file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces a C++ source file containing the meta-object code for those classes. Among other things, meta-object code is required for the signals and slots mechanism, the run-time type information, and the dynamic property system.
元对象编译器 (moc) 是处理 Qt 的 C++ 扩展的程序。
moc 工具读取 C++ 头文件。如果它找到一个或多个包含 Q_OBJECT 宏的类声明,它会生成一个 C++ 源文件,其中包含这些类的元对象代码。其中,信号和槽机制、运行时类型信息和动态属性系统需要元对象代码。
回答by Martin Beckett
It simply tells the pre-compiler that this class has gui elements and needs to be run through the 'moc' you only need to add this to classes that use the signal/slot mechanism.
But it will be quietly ignored in any other classes - it just adds to the build time.
它只是告诉预编译器此类具有 gui 元素并且需要通过“moc”运行,您只需将其添加到使用信号/插槽机制的类中。
但它会在任何其他类中被悄悄忽略——它只会增加构建时间。
回答by aditya
The MOC (meta object compiler) converts the Q_OBJECT macro included header files in to C++ equivalent source code. It basically controls the signal-slot mechanism, and makes it understandable to the C++ compiler
MOC(元对象编译器)将 Q_OBJECT 宏包含的头文件转换为 C++ 等效源代码。它基本上控制信号槽机制,并使 C++ 编译器可以理解
回答by Bob Zhang
1 From Qt documentation of The Meta-Object System
1 来自元对象系统的Qt 文档
The moc tool reads a C++ source file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces another C++ source file which contains the meta-object code for each of those classes. This generated source file is either #include'd into the class's source file or, more usually, compiled and linked with the class's implementation.
moc 工具读取 C++ 源文件。如果它找到一个或多个包含 Q_OBJECT 宏的类声明,它会生成另一个 C++ 源文件,其中包含每个这些类的元对象代码。这个生成的源文件要么 #include 到类的源文件中,要么更常见的是编译并与类的实现链接。
2 From Qt documentation of THE Q_OBJECT
2 来自Q_OBJECT 的Qt 文档
The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.
Q_OBJECT 宏必须出现在声明自己的信号和槽或使用 Qt 元对象系统提供的其他服务的类定义的私有部分中。
3 From Qt documentation of moc
3 来自moc 的Qt 文档
The moc tool reads a C++ header file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces a C++ source file containing the meta-object code for those classes. Among other things, meta-object code is required for the signals and slots mechanism, the run-time type information, and the dynamic property system.
moc 工具读取 C++ 头文件。如果它找到一个或多个包含 Q_OBJECT 宏的类声明,它会生成一个 C++ 源文件,其中包含这些类的元对象代码。其中,信号和槽机制、运行时类型信息和动态属性系统需要元对象代码。
4 From Qt documentation of Signals and Slots
4 来自信号和插槽的Qt 文档
The Q_OBJECT macro is expanded by the preprocessor to declare several member functions that are implemented by the moc; if you get compiler errors along the lines of "undefined reference to vtable for LcdNumber", you have probably forgotten to run the moc or to include the moc output in the link command.
Q_OBJECT 宏由预处理器展开,声明几个由 moc 实现的成员函数;如果您在“未定义对 LcdNumber 的 vtable 的引用”这一行中遇到编译器错误,您可能忘记了运行 moc 或在链接命令中包含 moc 输出。
回答by Arne
In gcc with -E
you can see expanded macros. This is what Q_OBJECT
expands into on gcc on Linux. Be aware, this might be platform dependent and it might change depending on the version of QT. You can see it is not just a tag for the moc compiler.
在 gcc with-E
你可以看到扩展的宏。这就是Q_OBJECT
在 Linux 上的 gcc 上扩展的内容。请注意,这可能取决于平台,并且可能会根据 QT 的版本而改变。你可以看到它不仅仅是 moc 编译器的标签。
# 11 "mainwindow.hh"
#pragma GCC diagnostic push
# 11 "mainwindow.hh"
# 11 "mainwindow.hh"
#pragma GCC diagnostic ignored "-Wsuggest-override"
# 11 "mainwindow.hh"
static const QMetaObject staticMetaObject; virtual const QMetaObject *metaObject() const; virtual void *qt_metacast(const char *); virtual int qt_metacall(QMetaObject::Call, int, void **); static inline QString tr(const char *s, cons
t char *c = nullptr, int n = -1) { return staticMetaObject.tr(s, c, n); } __attribute__ ((__deprecated__)) static inline QString trUtf8(const char *s, const char *c = nullptr, int n = -1) { return staticMetaObject.tr(s, c, n); } private:
# 11 "mainwindow.hh"
#pragma GCC diagnostic ignored "-Wattributes"
# 11 "mainwindow.hh"
__attribute__((visibility("hidden"))) static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **);
# 11 "mainwindow.hh"
#pragma GCC diagnostic pop
# 11 "mainwindow.hh"
struct QPrivateSignal {};
回答by Andres
The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.
Q_OBJECT 宏必须出现在声明自己的信号和槽或使用 Qt 元对象系统提供的其他服务的类定义的私有部分中。