Signals passing pointer
-
I wish to pass a pointer by singals and slots.
Connections{ target: device onImageBufCreated:{ real_time_image.initImageBuf(pFrameBuf) } }
The device issues a ImagebufCreated signals which carries a pointer to a buff. pFrame is a char* pointer.
The real_time_image component catch the signal to do somthing.
But this gives a runtime error:
qrc:/qml/main.qml:149: Error: Unknown method parameter type: char*When I change the pointer pFrame to int, the connection works fine. I also experimented with QObject::Connect, signals can carry pointers, but when it is defined in QML Connection type, the engine does not recognize it.
Is it a bug or is there a better practice than passing pointers?
Cheers
Update: Currently, I walk around this problem by reinterpret char* to int, passing it, then reinterpret it back. It works but Soooooo ugly.
-
@mondo said in Signals passing pointer:
I expect you need to register your c++ type that gets passed to your QML engine (pFrameBuf)
http://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html#registering-non-instantiable-typesHowever, have you seen :
http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#qbytearray-to-javascript-arraybuffer
http://doc.qt.io/qt-5/qbuffer.htmlYou might find it easier to not use char*'s.
-
@mondo said in Signals passing pointer:
I expect you need to register your c++ type that gets passed to your QML engine (pFrameBuf)
http://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html#registering-non-instantiable-typesHowever, have you seen :
http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#qbytearray-to-javascript-arraybuffer
http://doc.qt.io/qt-5/qbuffer.htmlYou might find it easier to not use char*'s.
-
Just have to register the class type (not as a pointer).
It just needs know about your class type and what to refer to it as.
// register T qmlRegisterUncreatableType<ClassT>("MyClassT" ,1,0,"MyClassT", ""); // set QML instance to context context->setContextProperty(QLatin1Literal("MyClassTIstnaceNamedCpp"), &myCppUncreatableObject);
// QML
function something(){ ... // call cpp something MyClassTIstnaceNamedCpp.something(); } // QML Connection also: Connections { target: MyClassTIstnaceNamedCpp; onSomething: { print("cpp something happened"); } }
Sorry it's not all complete but ... hopefully gives you a peek. It's probably possible much easier.
You can also pass pointers to objects and have QML dynamically create registered CPP class instances - my example has cpp instanced objects and named context... sorry if I'm not explaining very well but I am actually procrastinating myself... on a tight deadline. I shouldn't! Anyhow, good luck.