Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Postpone connect of QML function to c++ Qt signal
QtWS25 Last Chance

Postpone connect of QML function to c++ Qt signal

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlsignal & slotc++ to qml
2 Posts 1 Posters 1.0k Views
  • 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.
  • R Offline
    R Offline
    rdeem
    wrote on 20 Dec 2015, 11:05 last edited by
    #1

    Hi,

    I have successfully managed to connect a QML callback function to a Qt signal being emitted from a C++ class using the code below:

    //QML
    onClicked: {
       CppClass.finished.connect(callback)
    }
     function callback() {
       console.log("Callback function called!")
    }
    //C++
    void CppClass::ready()
    {
        emit finished();
    }
    

    My problem is that I would like to have several functions connected to the signal, but only one at a time. To solve this i would like to register the functions to the CppClass which pushes the functions to a local stack so they later can be connected from within the CppClass. I can unfortunately not find out the type of the argument used in the connect function called from QML. Below is sample code for what I would like to do:

    //QML
    onClicked: {
       CppClass.registerFunction(callback)
    }
     function callback() {
       console.log("Callback function called!")
    }
    //C++
    void CppClass::registerFunction(void* function) // <--- Not sure what type to use here
    {
        m_stack.push(function);
    }
    void CppClass::ready()
    {
        void* function = m_stack.pop();
        connect(this, SIGNAL(finished()), <arguments to connect function here>);
        emit finished();
        disconnect(function);
    }
    
    1 Reply Last reply
    0
    • R Offline
      R Offline
      rdeem
      wrote on 1 Jan 2016, 20:01 last edited by
      #2

      I solved it without signal using callback function directly:

      Item {
         id: myItem
         onClicked: {
            CppClass.registerFunction(myItem, "callback")
         }
         function callback() {
            console.log("Callback function called!")
         }
      }
      //C++
      void CppClass::registerFunction(QObject* object, QString function)
      {
          struct CallbackInfo info;
          info.object = object;
          info.function = function;
          m_stack.push(info);
      }
      void CppClass::ready()
      {
          struct CallbackInfo info = m_callbackStack.pop();
          QMetaObject::invokeMethod(info.object, info.function.toLatin1().constData());
      }
      
      1 Reply Last reply
      0

      • Login

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