In Qt, connecting signals from C classes to QML slots can sometimes encounter errors when dealing with non-primitive types. For instance, attempting to pass a QString from a C signal to a QML slot may result in the error: "Object::connect: No such slot QDeclarativeRectangle_QML_2::updateViewWithItem(QString)".
To resolve this issue, utilize QML connections instead of the traditional QObject::connect method. Here's how to implement it:
Add the object to QML: In your C code, expose the myObj object to QML using setContextProperty.
<code class="cpp">qmlVectorForm->rootContext()->setContextProperty("YourObject", myOb);</code>
Define the signal: In your C class, declare the signal as follows:
<code class="cpp">finishedGatheringDataForItem(QString signalString)</code>
Add Connections in QML: In your QML file, define the connections like this:
<code class="qml">Connections { target: YourObject onFinishedGatheringDataForItem: { qmlString = signalString } }</code>
By following these steps, you can establish a connection between the C signal and the QML slot, allowing you to pass and handle custom data types seamlessly.
The above is the detailed content of How to Pass Non-Primitive Types Between C Signals and QML Slots?. For more information, please follow other related articles on the PHP Chinese website!