1
votes

I've made a MyComponent QML component with some property:

Q_PROPERTY(PendingList* list MEMBER list)

and a function:

Q_INVOKABLE void send();

It can be created in QML:

MyComponent {
    id: myComponent
}

When I call somewhere the myComponent.send() while the list property is not defined, how can I properly report the problem in the stderr? I'd like to see the *.qml file name and line number where send() was called or the line number of the myComponent creation.

Is there a proper way to maybe get the QML stack trace or generate QQmlError or throw an exception that will be handled by the QML engine?

1
You can qmlEngine(this)->evaluate a javascript snippet that trows an exception, assuming that this is your component deriving ultimately from QObject. - Kuba hasn't forgotten Monica
It's a new instance of QmlEngine? I don't think it'll show any line numbers outside that snippet. - Velkan

1 Answers

0
votes

Will have to use some private stuff.

QTDD14 - Categorized logging in QML - Giuseppe D'Angelo

Git repository with the slides and the code: qmllogging

Short Version

Add to CMakeLists.txt:

include_directories(${Qt5Quick_PRIVATE_INCLUDE_DIRS})

Add some QV8Engine stuff to your Q_INVOKABLE function or slot:

#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));
}

Geting the engine for that function:

QQuickView view;
m_engine = view.engine();

Also set the message pattern to actually show the line number (somewhere in the beginning of main.cpp is fine):

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