Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Callback qml function from c++ invoke segfault
Forum Updated to NodeBB v4.3 + New Features

Callback qml function from c++ invoke segfault

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnatoliyL
    wrote on 6 Jun 2014, 12:56 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on 6 Jun 2014, 13:40 last edited by
      #2

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AnatoliyL
        wrote on 6 Jun 2014, 14:00 last edited by
        #3

        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

        1 Reply Last reply
        0
        • T Offline
          T Offline
          t3685
          wrote on 10 Jun 2014, 14:42 last edited by
          #4

          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?

          1 Reply Last reply
          0

          1/4

          6 Jun 2014, 12:56

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved