C++ 如何检查 QT_VERSION 以包含不同的标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24899558/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:50:37  来源:igfitidea点击:

how to check QT_VERSION to include different header?

c++qt

提问by Wang

Qt4 and Qt5 have different organization of header files. So I need to check the qt version to include different things, for example:

Qt4 和 Qt5 的头文件组织不同。所以我需要检查 qt 版本以包含不同的内容,例如:

#if QT_VERSION >= 0x050000
    #include <QtMultimedia>
#endif

however, this seems does not work. The QT_VERSION has not been defined. How can I solve this problem?

然而,这似乎不起作用。QT_VERSION 尚未定义。我怎么解决这个问题?

回答by rightaway717

As soon as you include <QtGlobal>you can check that with macros:

一旦您包含在内,<QtGlobal>您就可以使用宏进行检查:

#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)

It's the same as @Javier suggested, just a different, more readable to me, syntax

它与@Javier 建议的相同,只是一种不同的,对我来说更具可读性的语法

回答by Javier Cordero

As @Tay2510 commented, QT_VERSIONis declared inside of <QtGlobal>. As a result, you must type #include <QtGlobal>or another header that includes <QtGlobal>, before the version dependent preprocessor directives.

正如@Tay2510 所评论的,QT_VERSION<QtGlobal>. 因此,您必须在依赖版本的预处理器指令之前键入#include <QtGlobal>或 包含<QtGlobal>, 的其他标头。

<QObject>and <QCoreApplication>may also be used to access QT_VERSION.

<QObject>并且<QCoreApplication>还可以被用于访问QT_VERSION

Here is an example of the code:

以下是代码示例:

#include <QtGlobal>
#if QT_VERSION >= 0x050000
    #include <QApplication>
#else
    #include <QtGui/QApplication>
#endif

Remember that the qmake project should also handle version variations. Here are changes that should be done when using the QT multimedia library:

请记住,qmake 项目还应该处理版本变化。 以下是使用 QT 多媒体库时应进行的更改

The .pro file should look something like:

.pro 文件应该类似于:

QT += core gui

greaterThan(4, QT_MAJOR_VERSION): QT += widgets multimedia
lessThan(5, QT_MAJOR_VERSION): CONFIG += mobility
lessThan(5, QT_MAJOR_VERSION): MOBILITY += multimedia