I'm trying to transfer some QString
from c++
to QML
. Here are the steps I've done so far.
- Created a class inherited from
QObject
and definedsignal
in it.
class exampleClass: public QObject {
Q_OBJECT
signals:
void mySignal (QString myStr);
}
- Somewhere in the class implementation, I
emit
thesignal
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 toQML
i.e.
int main(...) {
.
.
exampleClass *exampleObj= new exampleClass(&app);
Engine.rootContext()->setContextProperty("exampleObj", exampleObj);
}
- And finally, in
QML
i have connected thesignal
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
}
}
}