Sending signals from CPP to QML using static functions.
-
Currently i'm using a libary thats giving me callbacks when the data is ready.
When the callback function fired it should send a signal to QML so it can change the UI. But its not reacting on my signal that im sending.. When the signal is send from normal timer its working well. But from static functions its just not working.
For the class im using singleton to access the signals.
MyClass* MyClass::m_instance = nullptr; MyClass* MyClass::instance(){ if (!m_instance) { m_instance = new MyClass(); } return m_instance; } void MyClass::callBack(){ // Do Some calculations emit instance()->CallBackEvent(); }
The header including the signal:
signals: void CallBackEvent();
My QML file looks like:
Connections { target: MyClass function onCallBackEvent(){ console.log("I got called! Change data!!!") } }
When i fire the signal by using a timer it works fine (non static function).
What might be the problem why my signal is not triggerd?
-
@DrWhoozy said in Sending signals from CPP to QML using static functions.:
What might be the problem why my signal is not triggerd
static, probably. Connections always relay on concrete object instances -> no static signals -> signals can not be emitted from static functions
-
@DrWhoozy not entirely sure, I think you can bypass this by wrapping the call-back in a lambda ? 🤷♂️
Besides that, see this SO thread I found about Qt Signals and Callbacks:
https://stackoverflow.com/questions/41771848/qt-emit-signal-from-callbackIt contains two valid solutions, as far as I can tell