Couldn't catch signal from Cpp in QML
-
I'm trying to transfer some QString from cpp to qml. Here are the steps I've done so far.
- Created a class inherited from QObject and defined signal in it.
class exampleClass: public QObject { Q_OBJECT . . signals: void mySignal (QString myStr); }
- Somewhere in the class implementation, i emit the signal i.e.
void exampleClass::exampleFtn( . . . ){ . . qDebug("Before transmitting Signal"); // This gets triggered emit mySignal ("Hello from CPP"); }
- In my main.cpp, I've initialized the class and expose it to QML i.e.
int main(...) { . . exampleClass *exampleObj= new exampleClass(&app); Engine.rootContext()->setContextProperty("exampleObj", exampleObj); }
- And finally, in QML i have connected the signal i.e.
Window { . . Connections { target: exampleObj function mySignal(myStr) { console.log("signal from C++ recieved !") // Doesn't get triggered console.log(myStr) // Doesn't get triggered } }
-
I'm trying to transfer some QString from cpp to qml. Here are the steps I've done so far.
- Created a class inherited from QObject and defined signal in it.
class exampleClass: public QObject { Q_OBJECT . . signals: void mySignal (QString myStr); }
- Somewhere in the class implementation, i emit the signal i.e.
void exampleClass::exampleFtn( . . . ){ . . qDebug("Before transmitting Signal"); // This gets triggered emit mySignal ("Hello from CPP"); }
- In my main.cpp, I've initialized the class and expose it to QML i.e.
int main(...) { . . exampleClass *exampleObj= new exampleClass(&app); Engine.rootContext()->setContextProperty("exampleObj", exampleObj); }
- And finally, in QML i have connected the signal i.e.
Window { . . Connections { target: exampleObj function mySignal(myStr) { console.log("signal from C++ recieved !") // Doesn't get triggered console.log(myStr) // Doesn't get triggered } }
@johndummy you've got it almost correct. The only error I see is that in QML, inside of
Connections
, you should make the function name match your signal. So:Connections { target: exampleObj function mySignal(myStr) { console.log("signal from C++ recieved !") // Doesn't get triggered console.log(myStr) // Doesn't get triggered } }
-
@johndummy you've got it almost correct. The only error I see is that in QML, inside of
Connections
, you should make the function name match your signal. So:Connections { target: exampleObj function mySignal(myStr) { console.log("signal from C++ recieved !") // Doesn't get triggered console.log(myStr) // Doesn't get triggered } }
@sierdzio : That's a typo error in the post. sorry for that.
My function name match exactly with my signal. The qml doesn't give me runtime error or warnings. I still don't get why i'm not able to capture the signal.
P.S: I will edit my initial post to correct the typo (if you don't mind).
-
@johndummy you've got it almost correct. The only error I see is that in QML, inside of
Connections
, you should make the function name match your signal. So:Connections { target: exampleObj function mySignal(myStr) { console.log("signal from C++ recieved !") // Doesn't get triggered console.log(myStr) // Doesn't get triggered } }
I changed mySignal to onMySignal and it worked now.
function onMySignal(myStr) { console.log("signal from C++ recieved !") console.log(myStr) }
Even onmySignal doesn't work. One has to capitalize the first alphabet even if its declaration doesn't have uppercase first latter.
-
-
I changed mySignal to onMySignal and it worked now.
function onMySignal(myStr) { console.log("signal from C++ recieved !") console.log(myStr) }
Even onmySignal doesn't work. One has to capitalize the first alphabet even if its declaration doesn't have uppercase first latter.
@johndummy said in Couldn't catch signal from Cpp in QML:
One has to capitalize the first alphabet even if its declaration doesn't have uppercase first latter
Not that this is documented somewhere.