Important: Please read the Qt Code of Conduct -

Callback qml function from c++ invoke segfault

  • I'm implementing callback of qml function in c++ code.

    I have two sides:
    One one side - qml project with gui and c++ plugin with dbus interface.

    On other side - something that just process incomming dbus messages and answer.

    I want to invoke some function in qml code:

        @Component.onCompleted: {
        A.request1( function(arg) {console.log('Callback func invoked - ', arg)} )@

    In c++ plugin i do this:
    class A () {
    request1(QJSValue callback_func) {
    QList<QVariant> args;
    QDBusPendingCall pcall = interface->
    asyncCallWithArgumentList(QString("GetMachineId"), args);
    QDBusPendingCallWatcher watcher =
    new QDBusPendingCallWatcher(pcall, this);
    this, SLOT(response1(QDBusPendingCallWatcher*)));
    hmi_callback_map_.insert(watcher, callback_func);

            qjs_value = callback_func;
   << 'Some text');--             
        public slots:
          void response1(QDBusPendingCallWatcher* w) {
            cout << "Debug message1";
            QMap<QDBusPendingCallWatcher*, QJSValue>::iterator it =
            QJSValue func = it.value();
   << 'Some text');
          QJSValue qjs_value;  
          std::map<QDBusPendingCallWatcher*, QJSValue*> map;


    Dbus send message, receive answer, Watcher catch reply, initiate SIGNAL, that connected to SLOT "response1".
    I see debug message "Debug message1", so i think "response1()" function is invoked correctly. But that, on next line

         @ << 'Some text');@

    i get a segfault and i can't understand the reason of it.

    One more thing: if i invoke callback in request1() function - it works correctly and i see "Callback func invoked - Some text". But it doesn't work in way that i described.

    Do you have some ideas?

  • Are you trying to have a qml element react a signal that your watcher class "A" is emitting?

  • Ok, updated code in first post.

    I try to:

    1. From qml call function in A class with callback function in argument. Callback function, that passed in arguments should be launched from c++ code later.
    2. Function in c++, that was initiated by qml should make an acync dbus call, than save to map watcher, which looks for answer + callback function, which should bee initiated, when we receive answer on dbus call.
    3. When answer from dbus comes, watcher catch it and invoke slot (response1) which he connected to.
    4. In slot function we should take callback from map (watcher is key) and launch it.

    But it crashes with segfault, when callback function is called

  • Is your callback function written in Javascript?

    Do you want to do this:

    1. qml calls c++ function to put message on DBUS
    2. when answer arrives, a javascript function has to be performed

    Is this correct?

Log in to reply