Qt 要包含哪些标头?

发布于 2024-11-09 09:00:23 字数 311 浏览 0 评论 0原文

使用 Qt 使用 C++ 进行编程时,我应该包含哪些内容?只包含 2 个

#include <QtCore>
#include <QtGui>

还是

#include <QWidget>
#include <QDialog>
#include <QList>
#include <QKeyEvent>
#include <QObject>

每个班级都包含 2 个?

谢谢!

While programming in C++ with Qt what includes should I make? Just 2 includes

#include <QtCore>
#include <QtGui>

or

#include <QWidget>
#include <QDialog>
#include <QList>
#include <QKeyEvent>
#include <QObject>

for every class?

Thanks!

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

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

发布评论

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

评论(4

傻比既视感 2024-11-16 09:00:24

仅包含您需要的类的定义 - 其他任何内容都不仅仅是懒惰,而且非常浪费,而且在我看来,风格很糟糕。

包括 QtGui(它本身包括 QtCore)将导致在您的编译中添加大约 350(!) 个头文件,而在您的示例中您只需要 6 个。编译将花费更长的时间,并且当有人尝试维护您的应用程序并浏览您的文件时他们将无法仅从包含中推断出每个文件/类正在尝试执行的操作 - 他们必须阅读整个源代码才能获得想法。

Include only the definitions of the classes you need - anything else isn't just lazy, it's extremely wasteful and to my mind bad style.

Including QtGui (which itself includes QtCore) will lead to adding about 350(!) header files to your compilation, when in your example you only needed 6. Compiling will take longer, and when someone tries to maintain your app and is browsing your files they won't be able to infer from just the includes what exactly it is each file/class is trying to do - they'll have to read the entire source to get an idea.

云裳 2024-11-16 09:00:24

只需在类中包含您需要的内容即可加快编译速度。如果您是懒人之一,并且不在乎编译需要多长时间,请选择更通用的标头,但如果您正在寻求优化,请避免使用它们(编译器/链接器很可能会删除未使用的内容,但它仍然是最好先不要包含它)。

一般来说,如果我需要仍然缺少的东西(即尚未包含),我只会包含新内容。所以,只需点击编译即可。如果缺少您忘记的内容,请添加标题。如果您不喜欢这种方法,请使用您提到的通用标头。

Just include what you need in your classes to speed up compiling. If you're one of the lazy guys and don't matter how long it takes to compile, pick the more generic headers but avoid them if you're looking for optimization (the compiler/linker will most likely remove unused stuff but it's still better to not include it first).

In general I only include new stuff if I need something that's still missing (i.e. not included yet). So, just hit compile. If it's missing something you forgot, add the header. If you don't like that approach, use the generic headers you mentioned.

御守 2024-11-16 09:00:24

如果您不想费心包含每个单独的类,则包含整个组件:

#include <QtCore>
#include <QtGui>

以及您需要使用的任何其他 QT 组件

#include <QtNetwork>

等。

如果您想显式指定您的依赖项,则包含

#include <QWidget>
#include <QDialog>
#include <QList>
#include <QKeyEvent>

您正在使用的每个单独的类和其他 QT 类。

If you don't want to bother including each individual class then include entire components:

#include <QtCore>
#include <QtGui>

And any other QT component you need to use

#include <QtNetwork>

etc.

If you want to explicitly specify your dependencies then include every individual class

#include <QWidget>
#include <QDialog>
#include <QList>
#include <QKeyEvent>

and other QT classes you are using.

〃温暖了心ぐ 2024-11-16 09:00:24

唯一的区别在于编译时间。如果你不使用预编译QtGuiQtCore头文件,编译时间会受到很大影响,你应该尽量避免这种情况。

The only difference is in compilation time. If you don't use precompile QtGui and QtCore headers, the compilation time will suffer greatly, and you should try to avoid that.

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