QQmlApplicationEngine 不发出警告信号 [英] QQmlApplicationEngine not emitting warnings signal

查看:93
本文介绍了QQmlApplicationEngine 不发出警告信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 PyQt 应用程序中加载 QML 文件,我想在 QML 文件无效的情况下显示错误和警告消息.在这种情况下,QQmlApplicationEngine 应该发出 warnings 信号,但这似乎不会发生.在下面的代码中,如果 QML 文件包含错误,则不会加载它,但永远不会调用 display_warnings 插槽.我究竟做错了什么?PyQt 版本是 5.9.

I am loading a QML file in PyQt application and I would like to display error and warning messages in case QML file is not valid. QQmlApplicationEngine should emit warnings signal in that case, but that doesn't appear to happen. In code below, if QML file contains errors, it is not loaded but display_warnings slot is never called. What am I doing wrong? PyQt version is 5.9.

# -*- coding: utf-8 -*-
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine

def display_warnings(warnings):
    pass # Process warnings

if __name__ == '__main__':
    import os
    import sys
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    engine.warnings.connect(display_warnings)
    qml_filename = os.path.join(os.path.dirname(__file__), 'MainWindow.qml')
    engine.load(qml_filename)
    sys.exit(app.exec_())

推荐答案

我不知道它在 Python 中如何工作,但在 C++ 中效果很好,即.关于所有 QML 错误的报告:

I don't know how does it work in Python but in C++ that works well ie. reports about all QML errors:

QQmlApplicationEngine engine;
QObject::connect(&engine, &QQmlApplicationEngine::warnings, [=] (const QList<QQmlError> &warnings) {
    foreach (const QQmlError &error, warnings) {
        qWarning() << "warning: " << error.toString();
    }
});

请注意,在这种情况下,连接类型是 Qt::DirectConnection.实际上所有的错误都和控制台的一样.

Note that in this case the type of connection is Qt::DirectConnection. In fact all the errors are the same as console ones.

在我的测试中,此代码错误 anchors.centerIn: parnt 将报告两次,控制台错误一次,warnings 信号处理程序错误:

In my test the error this code error anchors.centerIn: parnt will be reported twice, one error by console and one error by warnings signal handler:

警告:qrc:/main.qml:13: ReferenceError: part is not defined"

warning: "qrc:/main.qml:13: ReferenceError: parnt is not defined"

qrc:/main.qml:13: ReferenceError: part is not defined

qrc:/main.qml:13: ReferenceError: parnt is not defined

所以你应该检查你的 QML 代码是否包含错误,并检查你的控制台是否产生一些警告.

So you should check if your QML code contains error at all and also check if your console produces some warnings.

这篇关于QQmlApplicationEngine 不发出警告信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆