Qt的QML语言是否提供任何类型的"严格"模式?特别是,我想要两个功能:
- 引用
undefined
或null
时应用程序崩溃(例如,当foo
是现有属性但未定义bar
时为foo = bar
,而当foo
为null时为foo.bar
) - "hard"断言(
console.assert
功能不会使应用程序崩溃)
1。使用qml皮棉
在构建设置中的所有.qml和.js文件上运行qmllint
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警告转换为致命日志:
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;
}
}
}
然后确保QtFatalMsg
类型的QDebug消息使应用程序崩溃。
3.控制台崩溃.assert()
console.assert()
会创建错误,但没有特定的错误检测。因此,调整第2点。以在出现错误时使应用程序崩溃。