From b5d874e36fd39fa6e57ff27db27ae0b029949749 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Fri, 21 Apr 2023 19:23:29 +0800 Subject: [PATCH] Fix the QT_REQUIRE_VERSION macro Rewrite the whole macro as a function and use QVersionNumber instead of manual parsing to make it more modern and more readable. Change-Id: I25498320ef6cd9f19be9267421e2727cd28cfd7c Reviewed-by: Axel Spoerl Reviewed-by: Volker Hilsheimer Reviewed-by: Qt CI Bot --- src/widgets/dialogs/qmessagebox.h | 38 +++++++++++++++++++------------ 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/widgets/dialogs/qmessagebox.h b/src/widgets/dialogs/qmessagebox.h index 72f0840e80..161719e180 100644 --- a/src/widgets/dialogs/qmessagebox.h +++ b/src/widgets/dialogs/qmessagebox.h @@ -5,9 +5,13 @@ #define QMESSAGEBOX_H #include - +#include #include +#include +#include +#include + QT_REQUIRE_CONFIG(messagebox); QT_BEGIN_NAMESPACE @@ -312,20 +316,24 @@ private: Q_DECLARE_OPERATORS_FOR_FLAGS(QMessageBox::StandardButtons) -#define QT_REQUIRE_VERSION(argc, argv, str) { QString s = QString::fromLatin1(str);\ -QString sq = QString::fromLatin1(qVersion()); \ -if ((sq.section(QChar::fromLatin1('.'),0,0).toInt()<<16)+\ -(sq.section(QChar::fromLatin1('.'),1,1).toInt()<<8)+\ -sq.section(QChar::fromLatin1('.'),2,2).toInt()<(s.section(QChar::fromLatin1('.'),0,0).toInt()<<16)+\ -(s.section(QChar::fromLatin1('.'),1,1).toInt()<<8)+\ -s.section(QChar::fromLatin1('.'),2,2).toInt()) { \ -if (!qApp){ \ - new QApplication(argc,argv); \ -} \ -QString s = QApplication::tr("Executable '%1' requires Qt "\ - "%2, found Qt %3.").arg(qAppName()).arg(QString::fromLatin1(\ -str)).arg(QString::fromLatin1(qVersion())); QMessageBox::critical(0, QApplication::tr(\ -"Incompatible Qt Library Error"), s, QMessageBox::Abort, 0); qFatal("%s", s.toLatin1().data()); }} +[[maybe_unused]] +static inline void qRequireVersion(int argc, char *argv[], QAnyStringView req) +{ + const auto required = QVersionNumber::fromString(req).normalized(); + const auto current = QVersionNumber::fromString(qVersion()).normalized(); + if (current >= required) + return; + std::unique_ptr application; + if (!qApp) + application = std::make_unique(argc, argv); + const QString message = QApplication::tr("Application \"%1\" requires Qt %2, found Qt %3.") + .arg(qAppName(), required.toString(), current.toString()); + QMessageBox::critical(nullptr, QApplication::tr("Incompatible Qt Library Error"), + message, QMessageBox::Abort); + qFatal().noquote() << message; +} + +#define QT_REQUIRE_VERSION(argc, argv, str) qRequireVersion(argc, argv, str); QT_END_NAMESPACE