Qt的QML语言提供了某种“严格”模式吗?特别是,我希望有两个特性:
undefined或null时崩溃(例如,当foo是现有属性但未定义bar时为foo = bar,而foo为null时为foo.bar )console.assert特性不会使应用程序崩溃)。发布于 2016-04-08 17:00:19
1.使用qml lint
在构建设置中的所有.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;
}
}
}然后确保QDebug类型的QtFatalMsg消息使应用程序崩溃。
3.在console.assert()上崩溃
console.assert()会创建错误,但并不是专门用于检测它们的。因此,请修改第2点,使应用程序在出错时崩溃。
https://stackoverflow.com/questions/36504358
复制相似问题