如何报告自定义QML组件的错误?

时间:2015-08-20 12:47:25

标签: qt qml qtquick2

我使用了一些属性创建了一个MyComponent QML组件:

Q_PROPERTY(PendingList* list MEMBER list)

和一个功能:

Q_INVOKABLE void send();

可以在QML中创建:

MyComponent {
    id: myComponent
}

当我在myComponent.send()属性未定义的情况下调用list某处时,如何在stderr中正确报告问题?我想查看调用send()的* .qml文件名和行号或myComponent创建的行号。

是否有正确的方法可以获取QML堆栈跟踪或生成QQmlError或抛出将由QML引擎处理的异常?

1 个答案:

答案 0 :(得分:0)

将不得不使用一些私人物品。

QTDD14 - Categorized logging in QML - Giuseppe D'Angelo

Git存储库包含幻灯片和代码:qmllogging

短版

添加到CMakeLists.txt

include_directories(${Qt5Quick_PRIVATE_INCLUDE_DIRS})

将一些QV8Engine内容添加到Q_INVOKABLE功能或插槽中:

#include <QtQml>

#include <private/qv4engine_p.h>
#include <private/qv8engine_p.h>

void Logger::log(const QString &message)
{
    const QV4::StackFrame frame = QV8Engine::getV4(m_engine)->currentStackFrame();

    QMessageLogger(qPrintable(frame.source),
                   frame.line,
                   qPrintable(frame.function)).warning("%s", qPrintable(message));
}

获取该功能的引擎:

QQuickView view;
m_engine = view.engine();

还设置消息模式以实际显示行号(main.cpp开头的某个位置正常):

qSetMessagePattern("%{file}:%{line} - %{message}");