<QtVersionChecks>

用于检查 Qt 版本的宏。更多内容...

Header: #include <QtVersionChecks>

宏

QT_VERSION
QT_VERSION_CHECK(major, minor, patch)

详细说明

宏文档

QT_VERSION

该宏展开后将生成一个与QT_VERSION_CHECK() 结构相同的数值,该数值指定了使用该宏的代码所编译时所用的 Qt 版本。 例如,如果您使用 Qt 6.1.2 编译应用程序,QT_VERSION 宏将展开为 `0x060102`,这与 `QT_VERSION_CHECK(6, 1, 2)` 相同。请注意,这未必与应用程序在运行时检测到的自身所用版本一致。

您可以使用 QT_VERSION 在可用时选择最新的 Qt 功能,否则回退到旧的实现。建议使用QT_VERSION_CHECK() 作为比较值。

示例:

#if QT_VERSION >= QT_VERSION_CHECK(4, 1, 0)
    QIcon icon = style()->standardIcon(QStyle::SP_TrashIcon);
#else
    QPixmap pixmap = style()->standardPixmap(QStyle::SP_TrashIcon);
    QIcon icon(pixmap);
#endif

另请参阅 QT_VERSION_STR 、QT_VERSION_CHECK() 和qVersion()。

QT_VERSION_CHECK(major, minor, patch)

将版本的major 、minor 和patch 这三个数字转换为一个整数,该整数包含这三个数字的所有信息。以十六进制表示时,该整数的形式为0xMMNNPP ,其中0xMM == major 、0xNN == minor ,以及0xPP == patch 。该整数可与另一个经过类似处理的版本ID进行比较。

示例:

#include <QtGlobal>

#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QtWidgets>
#else
#include <QtGui>
#endif

注意:参数 按常规方式作为整数读取,因此通常应以十进制形式书写(若以十六进制书写,则必须使用0x 前缀)。因此QT_VERSION_CHECK(5, 15, 0) 等于0x050f00 ,这也可以写成QT_VERSION_CHECK(5, 0xf, 0) 。

另请参阅 QT_VERSION 。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.