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?
QML Debugging Infrastructure. The Qt QML module provides services for debugging, inspecting, and profiling applications via a TCP port or a local socket. Note: The qmltooling plugins that are required for debugging and profiling QML applications on devices are automatically installed during Qt installation.
An unclaimed breakpoint represents a task to interrupt the debugged program and passes the control to you later. It has two states: pending and implanted .
Setting Up QML Debugging The process of setting up debugging for Qt Quick projects depends on the type of the project: Qt Quick UI or Qt Quick Application. To debug Qt Quick UI projects: Select Projects, and then select the QML check box in the Run Settings, to enable QML debugging.
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.
You need to know it in runtime or in compile time? Macros are used in compile time, QML is executed in runtime, so there are no difference for compiled application between "debug" and "release".
Solution:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With