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

New Contributor
Posts: 2
Registered: ‎05-08-2012
My Device: Bold 9700 & 64GB Playbook
My Carrier: T-Mobile
Accepted Solution

QML/C++ Calling problem

Hey guys. Just looking for a little help here. I know that this problem has been covered before, but I am a little lost. I have searched the forums and looked through the tutorials, but I havn't been able to to find the answer I've been looking for. I am trying to call a simple C++ function from my QML/Cascades UI and it won't work. The problem that I have is that I've seen code snipets where people have solved their concerns with this, but nothing as an explination as to the what or why of it. Anyway, here are some code snippets, please tell me what I'm missing here.


// Default empty project template import bb.cascades 1.0 // creates one page with a label // The button was added by me to change the text of the label Page { Container { layout: DockLayout {} Label { id: label1 text: qsTr("Huh?") textStyle.base: SystemDefaults.TextStyles.BigText verticalAlignment: VerticalAlignment.Center horizontalAlignment: HorizontalAlignment.Center } Button { text: "Say Hi..." onClicked: { label1.text=HelloWorld.returnMyString(); //Works perfectly if written label1.text="Hello World"; } } } }



// Default empty project template
#include "HelloWorld.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

using namespace bb::cascades;

HelloWorld::HelloWorld(bb::cascades::Application *app)
: QObject(app)
    // create scene document from main.qml asset
    // set parent to created document to ensure it exists for the whole application lifetime
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

    // create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();
    // set created root object as a scene

//The only thing I added to the file was this function
QString HelloWorld::returnMyString()
	return ("Hello World");



// Default empty project template
#ifndef HelloWorld_HPP_
#define HelloWorld_HPP_

#include <QObject>

namespace bb { namespace cascades { class Application; }}

 * @brief Application pane object
 *Use this object to create and init app UI, to create context objects, to register the new meta types etc.
class HelloWorld : public QObject
    HelloWorld(bb::cascades::Application *app);
    virtual ~HelloWorld() {}

    //Added the ability to invoke the function from QML
    Q_INVOKABLE QString returnMyString();

#endif /* HelloWorld_HPP_ */

 I just don't know what I'm missing here. From what I've read in other posts, I've done what looks to be correct, but if I call it like I've written it, then the function apparently never gets called and nothing happens. I do not get any error codes while compiling and it runs on the sim prefectly, except for the inability to call the C++ function.


If you guys can give me a little guidence, I think i could really get off and running with the coding of my actual app. I know it's got to be something dumb that I'm missing.


Thanks Guys!

Regular Contributor
Posts: 77
Registered: ‎01-27-2010
My Device: Dev Alpha
My Carrier: Verizon

Re: QML/C++ Calling problem


You must give your qml to know your c++ object like this before accessing a c++ object from qml. Smiley Happy

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);



New Contributor
Posts: 2
Registered: ‎05-08-2012
My Device: Bold 9700 & 64GB Playbook
My Carrier: T-Mobile

Re: QML/C++ Calling problem

Worked perfectly! Thank you! I knew it had to be something simple that I was missing. Sometimes you just need a second set of eyes...especially when you're first learning.


Thanks again!