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 () {
    public:
    request1(QJSValue callback_func) {
    QList<QVariant> args;
    QDBusPendingCall pcall = interface->
    asyncCallWithArgumentList(QString("GetMachineId"), args);
    QDBusPendingCallWatcher watcher =
    new QDBusPendingCallWatcher(pcall, this);
    QObject::connect(watcher,
    SIGNAL(finished(QDBusPendingCallWatcher
    )),
    this, SLOT(response1(QDBusPendingCallWatcher*)));
    hmi_callback_map_.insert(watcher, callback_func);

            qjs_value = callback_func;
            qjs.value.call(QJSValueList() << 'Some text');--             
          }
        public slots:
          void response1(QDBusPendingCallWatcher* w) {
            cout << "Debug message1";
            QMap<QDBusPendingCallWatcher*, QJSValue>::iterator it =
              map_.find(w);
            QJSValue func = it.value();
            func.call(QJSValueList() << 'Some text');
          }
        private: 
          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

         @    qjs_value.call(QJSValueList() << 'Some text');@
    

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

    One more thing: if i invoke callback qjs_value.call 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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.