How to call a QML function from c++ code in BB10.?
My QML function-
function loadingData(data) {
evaluateJavaScript("createChart('" + data + "')")
}
Now how can I call this "loadingData" function from c++.
Please help...
How to call a QML function from c++ code in BB10.?
My QML function-
function loadingData(data) {
evaluateJavaScript("createChart('" + data + "')")
}
Now how can I call this "loadingData" function from c++.
Please help...
First, not clear on BB10, what i know is on desktop, but the method maybe no difference;
two way:
1) signal and slot; refer to http://qt-project.org/doc/qt-4.8/qtbinding.html#receiving-signals
2) metaObject
example(qt 4.8):
you would need an ID in your QML element, so that we can get the object in C++:
QDeclarativeView* mpView = //get view from whatever function
QObject *rootObject = mpView->rootObject(); //this is the rootObject of QML view
QObject *obj = rootObject->findChild<QObject *>(/*your element ID*/);
if (obj != NULL)
{
QVariant data = // what you need put in;
bool ret = QMetaObject::invokeMethod(obj, "loadingData",
Q_ARG(QVariant, data));
if (!ret) QDebug<<"invoke failure.";
}
You can access property of QML element from C++ easily, but calling function is going to be difficult.
But instead you can emit a signal from c++ and connect a function in qml to signal and execute required code there.
Expose C++ object to QML
qmlDocument->setContextProperty("_someClass", someClass);
which has signal defined say someSignal()
When ever you need to call QML function , emit someSignal()
emit someSignal()
In QML connect function to C++ signal
someClass.someSignal.connect(qmlFunction);
function qmlFunction() {
...
}
You can find details information here.