From 2ba2ae48b454e5a45351b01175f0927402e479ba Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Sun, 26 Apr 2026 19:35:59 -0700 Subject: [PATCH] core: add qt version check in preprocessor --- src/core/qmlglobal.cpp | 8 ++++++++ src/core/qmlglobal.hpp | 13 ++++++++++++- src/core/scanenv.cpp | 8 ++++++++ src/core/scanenv.hpp | 1 + 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/core/qmlglobal.cpp b/src/core/qmlglobal.cpp index 6cac3aa..0bee314 100644 --- a/src/core/qmlglobal.cpp +++ b/src/core/qmlglobal.cpp @@ -18,8 +18,10 @@ #include #include #include +#include #include #include +#include #include #include @@ -343,6 +345,12 @@ bool QuickshellGlobal::hasVersion(qint32 major, qint32 minor) { return QuickshellGlobal::hasVersion(major, minor, QStringList()); } +bool QuickshellGlobal::hasQtVersion(int major, int minor) { + auto qtVersion = QVersionNumber::fromString(qVersion()); + auto requiredVersion = QVersionNumber(major, minor); + return qtVersion >= requiredVersion; +} + QuickshellGlobal* QuickshellGlobal::create(QQmlEngine* engine, QJSEngine* /*unused*/) { auto* qsg = new QuickshellGlobal(); auto* generation = EngineGeneration::findEngineGeneration(engine); diff --git a/src/core/qmlglobal.hpp b/src/core/qmlglobal.hpp index 72055df..2887a93 100644 --- a/src/core/qmlglobal.hpp +++ b/src/core/qmlglobal.hpp @@ -244,7 +244,7 @@ public: /// it is assumed that all unreleased features are present. The unreleased feature list /// may be omitted. /// - /// > [!NOTE] You can feature gate code blocks using Quickshell's preprocessor which + /// > [!NOTE] You can version gate code blocks using Quickshell's preprocessor which /// > has the same function available. /// > /// > ```qml @@ -254,6 +254,17 @@ public: /// > ``` Q_INVOKABLE static bool hasVersion(qint32 major, qint32 minor, const QStringList& features); Q_INVOKABLE static bool hasVersion(qint32 major, qint32 minor); + /// Check if Qt's version is at least `major.minor`. + /// + /// > [!NOTE] You can version gate code blocks using Quickshell's preprocessor which + /// > has the same function available. + /// > + /// > ```qml + /// > //@ if hasVersion(6, 10) + /// > ... + /// > //@ endif + /// > ``` + Q_INVOKABLE static bool hasQtVersion(qint32 major, qint32 minor); void clearReloadPopupInhibit() { this->mInhibitReloadPopup = false; } [[nodiscard]] bool isReloadPopupInhibited() const { return this->mInhibitReloadPopup; } diff --git a/src/core/scanenv.cpp b/src/core/scanenv.cpp index 047f472..270b238 100644 --- a/src/core/scanenv.cpp +++ b/src/core/scanenv.cpp @@ -2,6 +2,8 @@ #include #include +#include +#include #include "build.hpp" @@ -20,6 +22,12 @@ bool PreprocEnv::hasVersion(int major, int minor, const QStringList& features) { return QS_VERSION_MAJOR == major && QS_VERSION_MINOR == minor; } +bool PreprocEnv::hasQtVersion(int major, int minor) { + auto qtVersion = QVersionNumber::fromString(qVersion()); + auto requiredVersion = QVersionNumber(major, minor); + return qtVersion >= requiredVersion; +} + QString PreprocEnv::env(const QString& variable) { return qEnvironmentVariable(variable.toStdString().c_str()); } diff --git a/src/core/scanenv.hpp b/src/core/scanenv.hpp index c1c6814..68013b3 100644 --- a/src/core/scanenv.hpp +++ b/src/core/scanenv.hpp @@ -12,6 +12,7 @@ class PreprocEnv: public QObject { public: Q_INVOKABLE static bool hasVersion(int major, int minor, const QStringList& features = QStringList()); + Q_INVOKABLE static bool hasQtVersion(int major, int minor); Q_INVOKABLE static QString env(const QString& variable); Q_INVOKABLE static bool isEnvSet(const QString& variable);