In Qt / C ++, there is a QT_DEBUG macro to know when it compiles during debugging or release.
Is there a way to find out if an application running in debug mode o works in a QML file?
You can use context properties to open C ++ objects for QML:
#include <QtGui/QGuiApplication> #include <QQmlContext> #include <QQuickView> #include "qtquick2applicationviewer.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QtQuick2ApplicationViewer viewer; #ifdef QT_DEBUG viewer.rootContext()->setContextProperty("debug", true); #else viewer.rootContext()->setContextProperty("debug", false); #endif viewer.setMainQmlFile(QStringLiteral("qml/quick/main.qml")); viewer.showExpanded(); return app.exec(); }
main.qml:
import QtQuick 2.2 Item { id: scene width: 360 height: 360 Text { anchors.centerIn: parent text: debug } }
It is not possible to define this purely from within QML.
Do you need to know this at runtime or at compile time? Macros are used at compile time, QML is executed at runtime, so there is no difference for a compiled application between "debug" and "release".
Decision:
Create a class with const property declared in next way: class IsDebug : public QObject { QOBJECT Q_PROPERTY( IsDebug READ IsCompiledInDebug ) // Mb some extra arguments for QML access public: bool IsCompiledInDebug() const { return m_isDebugBuild; } IsDebug() #ifdef QT_DEBUG : m_isDebugBuild( true ) #else : m_isDebugBuild( false ) #endif {} private: const bool m_isDebugBuild; }