Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 701
Ricardo Avatar asked Apr 07 '14 07:04

Ricardo


People also ask

What is QML debugging?

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.

What is unclaimed breakpoint in Qt?

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 .

How do I debug QML in Visual Studio?

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.


2 Answers

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.

like image 170
Mitch Avatar answered Oct 21 '22 04:10

Mitch


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;
}
like image 41
Dmitry Sazonov Avatar answered Oct 21 '22 04:10

Dmitry Sazonov