【问题标题】:QtQuick, how to know if a application was compiled on debug or release mode?QtQuick,如何知道应用程序是在调试还是发布模式下编译的?
【发布时间】:2014-04-07 07:21:08
【问题描述】:

在 Qt/C++ 中有 QT_DEBUG 定义宏来知道它何时在调试或发布时被编译。

有什么方法可以知道应用程序是否在 QML 文件中以调试或发布模式运行?

【问题讨论】:

    标签: qt qml qt-quick qtquick2 qtquickcontrols


    【解决方案1】:

    您可以使用context properties 将 C++ 对象公开给 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
        }
    }
    

    不可能完全从 QML 中确定这一点。

    【讨论】:

    • 我使用了这种方法,但我将 var 名称设置为“QT_DEBUG”,因为明确 var 是 DEFINE。不过谢谢
    【解决方案2】:

    您需要在运行时或编译时了解它吗?宏在编译时使用,QML 在运行时执行,因此编译后的应用程序“调试”和“发布”没有区别。

    解决方案:

    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;
    }
    

    【讨论】:

    • 米奇的方案更简洁,不需要额外的QObject。
    • @FrankOsterfeld 1) 我的解决方案悬停在所有应用程序上,而不仅仅是 QML 部分。 2)我们同时发布了我们的答案。在我发布自己的之前,我没有看到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    相关资源
    最近更新 更多