InterviewSolution
| 1. |
How To Call A Qml Function From C++? |
|
Answer» QML functions can be called from C++ and vice-versa. All QML functions are EXPOSED to the meta-object SYSTEM and can be called USING QMetaObject::invokeMethod(). Here is a C++ application that uses this to call a QML function: // MyItem.qml import QtQuick 1.0 ITEM { function mysql Function(msg) { console.log("GOT message:", msg) return "some return value" } } // main.cpp QDeclarativeEngine engine; QDeclarativeComponent component(&engine, "MyItem.qml"); QObject *object = component.creator(); QVariant returned Value; QVariant msg = "Hello from C++"; QMetaObject::invokeMethod(object, "mysql Function", Q_RETURN_ARG(QVariant, returned Value), Q_ARG(QVariant, msg)); qDebug() << "QML function returned:" << returnedValue.toString(); delete object; QML functions can be called from C++ and vice-versa. All QML functions are exposed to the meta-object system and can be called using QMetaObject::invokeMethod(). Here is a C++ application that uses this to call a QML function: // MyItem.qml import QtQuick 1.0 Item { function mysql Function(msg) { console.log("Got message:", msg) return "some return value" } } // main.cpp QDeclarativeEngine engine; QDeclarativeComponent component(&engine, "MyItem.qml"); QObject *object = component.creator(); QVariant returned Value; QVariant msg = "Hello from C++"; QMetaObject::invokeMethod(object, "mysql Function", Q_RETURN_ARG(QVariant, returned Value), Q_ARG(QVariant, msg)); qDebug() << "QML function returned:" << returnedValue.toString(); delete object; |
|