Qt moc 在头文件中实现?
是否可以告诉 Qt MOC 我想声明该类并在单个文件中实现它,而不是将它们拆分为 .h 和 .cpp 文件?
Is it possible to tell the Qt MOC that I would like to declare the class and implement it in a single file rather than splitting them up into an .h and .cpp file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果要在 cpp 文件中声明并实现 QObject 子类,则必须手动包含 moc 文件。
例如:(文件main.cpp)
添加
#include
语句后必须重新运行moc(make qmake
)。If you want to declare and implement a QObject subclass in you cpp file, you have to manually include the moc file.
For example: (file main.cpp)
You have to rerun moc (
make qmake
) after adding the#include
statement.TL;DR
是的,如果您只谈论您自己编写的文件(而不是由 moc 生成的文件)。您根本不需要做任何特别的事情。
如果您希望在您编写的文件中显式包含 moc 输出,则在一种情况下您必须这样做,在另一种情况下您可能希望这样做。假设
MyObject
类在MyObject.h
中声明,并且它的定义在MyObject.cpp
中给出:MyObject.moc
必须包含在MyObject.cpp
的末尾当且仅当您声明的任何 <MyObject.cpp
中的 code>Q_OBJECT 类。moc_MyObject.cpp
可以包含在MyObject.cpp
任何地方,以将翻译单元的数量减半你的项目。这只是构建时优化。如果不这样做,moc_MyObject.cpp
将被单独编译。每次在任何源文件或头文件中添加或删除
Q_OBJECT
宏,或者在此类文件中添加或删除显式包含的 moc 输出时,都必须重新运行 qmake/cmake。要在 Qt Creator 中重新运行 qmake/cmake,只需右键单击顶层项目,然后从上下文菜单中选择运行 qmake 或运行 cmake
简单的答案
一个基于 qmake 的 Qt 项目示例可能包含三个文件,如下所示:
它没有做太多事情,但它肯定是有效的。除了构建系统链接我们的项目的各种库之外,还有两个特定于项目的 翻译单元:
main.cpp
和moc_myobject.cpp
。尽管看起来
MyObject
的整个实现都在头文件中,但实际上并非如此。Q_OBJECT
宏声明了一些未定义的实现,如果不是 moc 生成的定义的话。moc如何进入画面?首次构建项目时,元构建工具(qmake 或 cmake)会扫描所有 C++ 输入文件是否存在
Q_OBJECT
宏。那些含有它的,会受到特殊对待。在此示例项目中,myobject.h
包含Q_OBJECT
并通过 moc 处理为moc_myobject.cpp
。后者被添加到由 C++ 编译器编译的源列表中。仅从概念上讲,就好像.pro
文件中有SOURCES += moc_myobject.cpp
一样。 当然,您永远不应该将这样的行添加到 .pro 文件中。现在请注意,
MyObject
的整个实现位于两个文件中:<代码>myobject.h 和moc_myobject.cpp
。myobject.h
可以包含在任意数量的翻译单元中 - 因为不存在违反单一定义规则的类外(独立)定义。构建系统将moc_myobject.cpp
视为一个单独的翻译单元 - 这一切都为您处理好了。因此,您的目标完全不费吹灰之力就达到了:您无需执行任何特殊操作即可将
MyObject
的整个实现(除了 moc 生成的位)放入头文件中。它可能会延长编译时间,但在其他方面是无害的。违反规则的方法
确切地说,它违反了单一定义规则,因此产生无效的结果C++ 程序。
现在您可能会想变得“聪明”并强制将 moc 输出包含到头文件中。 qmake/cmake/qbs 将会适应,并且会检测到这一点,并且不会再通过编译器单独处理 moc 输出,就像您已经完成的那样。
因此,假设在上面的项目中,您将
myobject.h
更改为如下所示:按照目前的情况,该项目仍将编译,似乎实现了您的目标,即只有一个文件来定义整个项目
MyObject
- 您编写的位和 moc 生成的位。但这只是由于一种不太可能的令人高兴的情况:moc_*.cpp
的内容仍然仅在一个翻译单元中 -假设现在我们向项目中添加了第二个源文件:
没什么大不了的。即使作用不大,它也应该有效,对吗?
唉,链接不了。现在,
moc_myobject.cpp
的内容是两个翻译单元的一部分。由于moc_myobject.cpp
的内部充满了独立的类成员定义,这违反了关于在 .cpp 文件中包含 moc 输出
正如 TL;DR 中提到的,在特定情况下,上述内容均不排除在源 (.cpp) 文件中显式包含 moc 输出。
给定“foo.h”和“foo.cpp”以及由 qmake 或 cmake 管理的项目,构建系统将指示
moc
生成最多两个输出:moc_foo。来自
,ifffoo.h
的 cppfoo.h
包含Q_OBJECT
宏。foo.moc
来自foo.cpp
,ifffoo.cpp
包含#include " foo.moc"
.让我们详细研究一下为什么要在 .cpp 文件中包含其中之一。
包括 xxx.moc
有时,特别是在 C++11 和 Qt 5 之前的日子里,声明小型助手 QObject 类仅在一个翻译单元(源文件)中本地使用会很方便。
在编写用于 stackoverflow 使用的单文件、独立测试用例和示例时,这也很方便。
假设您希望有人在一个文件中演示如何从事件循环中调用槽:
由于
MyObject
是一个小类,仅在main.moc
中本地使用,将其定义放入单独的头文件中没有多大意义。#include "main.moc"
行将被 qmake/cmake 注意到,并且main.cpp
将通过 moc 提供,从而生成main.moc< /代码>。由于
main.moc
定义了MyObject
的成员,因此它必须包含在声明MyObject
的位置。由于声明位于main.cpp
内,因此您不能让main.moc
成为单独的翻译单元:由于MyObject
它不会编译代码> 未声明。声明它的唯一位置是在main.cpp
中,靠近末尾的某个位置。这就是为什么在foo.cpp
末尾始终包含foo.moc
是一个安全的选择。精明的读者现在会问:
moc_foo.cpp
如何获取其定义的成员的类的声明?很简单:它显式包含生成它的头文件(此处:foo.h
)。当然,foo.moc
不能这样做,因为它会通过多重定义foo.cpp
中的所有内容来打破单一定义规则。包括 moc_xxx.cpp
在特别大的 Qt 项目中,每个类平均可能有两个文件和两个翻译单元:
MyObject.h
和MyObject。 cpp
是您编写的文件。MyObject.cpp
和moc_MyObject.cpp
是翻译单元。通过在
MyObject.cpp
中显式包含moc_MyObject.cpp
,可以将翻译单元的数量减半:TL;DR
Yes, if you're talking only about the files that you write yourself (as opposed as those generated by moc). You don't need to do anything special at all.
If you ever wish to include moc output explicitly in the files you write, there is one case in which you must do it, and one case where you might wish to do it. Let's assume that
MyObject
class is declared inMyObject.h
and your definition of it is given inMyObject.cpp
:MyObject.moc
must be included at the end ofMyObject.cpp
iff you declare anyQ_OBJECT
classes withinMyObject.cpp
.moc_MyObject.cpp
can be included anywhere inMyObject.cpp
to halve the number of translation units in your project. It's a build-time optimization only. If you don't do it,moc_MyObject.cpp
will be separately compiled.Every time you add or remove
Q_OBJECT
macro from any source or header file, or you add or remove explicit inclusions of moc output in such files, you must re-run qmake/cmake.To re-run qmake/cmake in Qt Creator, simply right-click on the toplevel project, and select Run qmake or Run cmake from the context menu.
The Simple Answer
An example qmake-based Qt project might consist of three files, as follows:
It doesn't do much, but it's certainly valid. Apart from the various libraries that the build system links our project with, there are two project-specific translation units:
main.cpp
andmoc_myobject.cpp
.Even though it appears that the entire implementation of
MyObject
is in the header file, it really isn't. TheQ_OBJECT
macro declares some bits of implementation that would be undefined, if it weren't for the moc-generated definitions.How does moc enter into the picture? When the project is first built, the metabuild tool - either qmake or cmake - scans all the C++ input files for the presence of
Q_OBJECT
macro. Those that contain it, are given special treatment. In this example project,myobject.h
containsQ_OBJECT
and is processed through moc intomoc_myobject.cpp
. The latter is added to the list of sources that are compiled by the C++ compiler. It is, only conceptually, as if you hadSOURCES += moc_myobject.cpp
in the.pro
file. Of course you should never add such a line to the .pro file.Now notice that the entire implementation of
MyObject
rests in two files:myobject.h
andmoc_myobject.cpp
.myobject.h
can be included in as many translation units as you desire - because there are no out-of-class (standalone) definitions that would violate the one definition rule. The build system treatsmoc_myobject.cpp
as a single, separate translation unit - this is all taken care of for you.Thus your goal is reached with no effort at all: You have nothing special to do to put the entire implementation of
MyObject
- save for the bits that moc produces - into the header file. It may prolong the compilation times, but is otherwise innocuous.A Rule-Violating Approach
It violates the one definition rule, to be exact, and thus yields an invalid C++ program.
Now you might think to get "clever" and forcibly include the moc output into the header file. The qmake/cmake/qbs will be accommodating, and will detect this and won't separately process moc output through the compiler anymore, as you've already done it.
So, suppose that, in the project above, you changed
myobject.h
to read as follows:As it stands, the project will still compile, seemingly fulfilling your goal of having just one file that defines the entirety of
MyObject
- the bits that you wrote, and the bits that moc generated, both. But it's only due to an unlikely happy circumstance: the contents ofmoc_*.cpp
are still in only one translation unit -Suppose, now that we add a second source file to our project:
Not much to it. It should work, even if it doesn't do much, right?
Alas, it won't link. Now the contents of
moc_myobject.cpp
are a part of two translation units. Sincemoc_myobject.cpp
's insides are full of stand-alone class member definitions, this violates the one definition rule. The rule mandates that stand-alone definitions may only appear in one translation unit within a target. The linker, being the guardian of this rule, rightly complains.On Including moc Output in the .cpp File
As alluded to in the TL;DR, none of the above precludes explicit inclusion of moc output in source (.cpp) files, in specific circumstances.
Given "foo.h" and "foo.cpp", and a project managed by qmake or cmake, the build system will direct
moc
to generate up to two outputs:moc_foo.cpp
fromfoo.h
, ifffoo.h
containsQ_OBJECT
macro.foo.moc
fromfoo.cpp
, ifffoo.cpp
contains#include "foo.moc"
.Let's examine in detail why would you want to include either one in a .cpp file.
Including xxx.moc
Sometimes, especially in the days prior to C++11 and Qt 5, it comes handy to declare small helper QObject classes for local use within one translation unit (source file) only.
This also comes handy when writing single-file, self-contained test cases and examples for stackoverflow use.
Suppose you wished someone to demonstrate, in one file, how to invoke a slot from the event loop:
Since
MyObject
is a small class that's only used locally inmain.moc
, it wouldn't make much sense to put its definition into a separate header file. The#include "main.moc"
line will be noticed by qmake/cmake, andmain.cpp
will be fed through moc, resulting inmain.moc
. Sincemain.moc
defines members ofMyObject
, it must be included someplace whereMyObject
is declared. As the declaration is withinmain.cpp
, you can't havemain.moc
be a separate translation unit: it won't compile due toMyObject
being not declared. The only place where it is declared is withinmain.cpp
, somewhere towards the end. That's why it's a safe bet to always includefoo.moc
at the end offoo.cpp
.An astute reader now asks: how come
moc_foo.cpp
gets the declarations of the classes whose members it defines? Quite simply: it explicitly includes the header file it is generated from (here:foo.h
). Of coursefoo.moc
can't do that, since it'd break the single definition rule by multiply defining everything infoo.cpp
.Including moc_xxx.cpp
In particularly large Qt projects, you might have - on average - two files and two translation units per each class:
MyObject.h
andMyObject.cpp
are the files you write.MyObject.cpp
andmoc_MyObject.cpp
are the translation units.It is possible to halve the number of translation units by explicitly including
moc_MyObject.cpp
somewhere inMyObject.cpp
:我认为您通常可以在头文件中声明并实现该类,而无需使用任何特殊内容,例如:
在此之后,您将头文件添加到 pri 文件并再次执行 qmake 就这样了。您有一个继承自 qobject 的类,并在 .h 文件中实现和声明。
I think you can normally declare and implement the class in the header file without using anything special, eg:
After this you add the header file to the pri file and execute qmake again and that's it. You have a class that inherits from qobject and is implemented and declared int he .h file.
我相信这是最好的方法。这实际上就是我现在构建所有对象的方式。
Qt 4.8.7
Works.pro:main.cpp
构建
Window.h
MyWidget.h
...
qmake Works.pro
制作
I believe this to be the best way. It's actually how I construct all of my objects now.
Qt 4.8.7
Works.pro:
main.cpp
Window.h
MyWidget.h
Build...
qmake Works.pro
make