Postpone connect of QML function to c++ Qt signal
Solved
QML and Qt Quick
-
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); }
-
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()); }