Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Philipk
Posts: 676
Registered: ‎05-06-2012
My Device: Dev Alpha & PlayBook
Accepted Solution

Call C++ and return value

main.qml

app.importGraph(selectedFile);

// I call a C++ function to get a file from the system

c++

void ApplicationUI::importGraph(QString fileName) {

    QString newGraph;

    // bunch of code adding the file contents to newGraph

    // Here I want to pass back newGraph to what ever QML made the call to importGraph C++ function, how is this done?

 

}

_________________________________________________________
co-founder of Diaree | BB Dev for PictureThis
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Call C++ and return value

Hi,

This can be done by simply returning it from the function:

 

QString ApplicationUI::importGraph(QString fileName) {
  ...
  return newGraph;
}

In QML:

var myString = app.importGraph(selectedFile)

 

importGraph should be declared as Q_INVOKABLE and "app" registered as contextProperty.

 

Optionally, a property can be used for tracking changes. It requires defining a getter, setter and a signal. There's sample code in "Exposing C++ object to QML" section on this page:

http://developer.blackberry.com/cascades/documentation/dev/integrating_cpp_qml/

 


Andrey Fidrya, @zmeyc on twitter
Developer
Philipk
Posts: 676
Registered: ‎05-06-2012
My Device: Dev Alpha & PlayBook

Re: Call C++ and return value

[ Edited ]

When I add

    return newGraph;

I get back

Multiple markers at this line
- return-statement with a value, in function returning 'void' [-
fpermissive]
- Return has value, in function returning void

_________________________________________________________
co-founder of Diaree | BB Dev for PictureThis
Developer
Philipk
Posts: 676
Registered: ‎05-06-2012
My Device: Dev Alpha & PlayBook

Re: Call C++ and return value

[ Edited ]

Sorry I didn't change void to QString. That was the problem.

But now I get another error

prototype for 'QString ApplicationUI::importGraph(QString)' does not match any in class
'ApplicationUI'

_________________________________________________________
co-founder of Diaree | BB Dev for PictureThis
Developer
Philipk
Posts: 676
Registered: ‎05-06-2012
My Device: Dev Alpha & PlayBook

Re: Call C++ and return value

Hadn't changed it in the .hpp file
_________________________________________________________
co-founder of Diaree | BB Dev for PictureThis