首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QML的“严格”模式?

QML的“严格”模式?
EN

Stack Overflow用户
提问于 2016-04-08 16:09:26
回答 1查看 843关注 0票数 1

Qt的QML语言提供了某种“严格”模式吗?特别是,我希望有两个特性:

  • 应用程序在引用undefinednull时崩溃(例如,当foo是现有属性但未定义bar时为foo = bar,而foo为null时为foo.bar )
  • “难”断言( console.assert特性不会使应用程序崩溃)。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-08 17:00:19

1.使用qml lint

在构建设置中的所有.qml和.js文件上运行qmllint

代码语言:javascript
复制
find ./myproject -type f -regex ".*\.\(qml\|js\)" -exec "$QT_DIR/bin/qmllint" \{\} +

2. QML错误/警告的崩溃应用程序

编写自定义的QDebug消息处理程序函数static void handler(QtMsgType type, const QMessageLogContext& context, const QString &message);,通过qInstallMessageHandler(&MyQDebugMessageHandler::handler);注册该函数将QML警告转换为致命日志:

代码语言:javascript
复制
if (type == QtWarningMsg)
{
    auto fatalWarnings = std::vector<QString>{
            QStringLiteral("ReferenceError:"),
            QStringLiteral("is not a type"),
            QStringLiteral("File not found"),
    };

    for (const auto &s : fatalWarnings)
    {
        if (message.contains(s))
        {
            type = QtFatalMsg;
            break;
        }
    }
}

然后确保QDebug类型的QtFatalMsg消息使应用程序崩溃。

3.在console.assert()上崩溃

console.assert()会创建错误,但并不是专门用于检测它们的。因此,请修改第2点,使应用程序在出错时崩溃。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36504358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档