1

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?

2
  • 1
    You can qmlEngine(this)->evaluate a javascript snippet that trows an exception, assuming that this is your component deriving ultimately from QObject. Commented Aug 20, 2015 at 19:26
  • It's a new instance of QmlEngine? I don't think it'll show any line numbers outside that snippet. Commented Aug 22, 2015 at 9:02

1 Answer 1

0

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}");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.