0

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...

user2085965
  • 393
  • 2
  • 13
  • 33
  • 1
    possible duplicate of [C++ SIGNAL to QML SLOT in Qt](http://stackoverflow.com/questions/8834147/c-signal-to-qml-slot-in-qt) – Michael Sep 10 '13 at 09:47

2 Answers2

2

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.";  
} 
RoyMuste
  • 21
  • 4
1

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.

Kunal
  • 3,475
  • 21
  • 14