I did a few things, but got stuck in one specific example. The code is similar to
Myitem.qml
import QtQuick 1.0 Item { function myQmlFunction(msg) { console.log("Got message:", msg) return "some return value" } }
main.cpp
QDeclarativeEngine engine; QDeclarativeComponent component(&engine, "MyItem.qml"); QObject *object = component.create(); QVariant returnedValue; QVariant msg = "Hello from C++"; QMetaObject::invokeMethod(object, "myQmlFunction", Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, msg)); qDebug() << "QML function returned:" << returnedValue.toString(); delete object;
Simple, but when I run this code in my Qt (5.0), it shows something like QDeclarativeComponent: the component is not ready.
I know something is missing. In google, I found that the method should be declared as Q_INVOKABLE, but I do not understand why?
c ++ qt qml
anbu selvan
source share