Q_OBJECT 宏有什么作用?为什么所有 Qt 对象都需要这个宏?

2021-12-09 00:00:00 qt macros c++

我刚开始使用 Qt 并注意到所有示例类定义都将宏 Q_OBJECT 作为第一行.这个预处理器宏的目的是什么?

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 文档:

元对象编译器,moc,是处理 Qt 的 C++ 的程序扩展.

The Meta-Object Compiler, moc, is the program that handles Qt's C++ extensions.

moc 工具读取 C++ 头文件.如果找到一个或多个类包含 Q_OBJECT 的声明宏,它生成一个 C++ 源文件包含元对象代码那些课.除其他事项外,需要元对象代码信号槽机制运行时类型信息,以及动态属性系统.

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.

相关文章