qtqmlqt-quickqtquick2qtquickcontrols

QtQuick, how to know if a application was compiled on debug or release mode?


At Qt/C++ there is QT_DEBUG define macro to know when it is compiled at debug or release.

Is there any method to know if is the application running in debug o release mode inside a QML file?


Solution

  • You can use context properties (or QQmlApplicationEngine::setInitialProperties() since Qt 5.14) to expose C++ objects to 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's not possible to determine this purely from within QML.