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
Posts: 130
Registered: ‎10-26-2010
My Device: Blackberry Bold 9700

QML and C++ intergration using setContextProperty()

Hi , 

 

I know myself my problem is really basic but I am unable to get to the root of it so posting it here.I followed all the steps of QML and C++ integration guidestill not success

 

I tried to integrate QML and C++ using the setContextProperty

 

TestApp::TestApp(bb::cascades::Application *app)
: QObject(app)
{
	  QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    NavigationPane *root = qml->createRootObject<NavigationPane>();
    rootPane = root ;
    qml->setContextProperty("MainApp" , this);
    app->setScene(root);
}

 This is my first attached to main.qml which isa navigain pane , From main,qml I call the action Item of the page anc push a new page is view its works fine and also call MainApp.setHandelr() .


here is the method 

void TestApp::attachContactHandler(){
	print("attach contact handler class");
	contactHandler = new ContactBookhandler (this) ;
}

 My Contact Handler is is very simple

 

#include "ContactBookhandler.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/Page>
#include <QString>
#include <bb/cascades/TextField>
#include <iostream.h>

using namespace bb::cascades;

ContactBookhandler::ContactBookhandler(QObject *app)
: QObject(app)
{
	print("inside contact handler constructor");
	QmlDocument *qml = QmlDocument::create("asset:///ContactBook.qml");
	    if(!qml->hasErrors())
	    {
	    	print("Contact handler: QML is free of error");

	    	qml->setContextProperty("Handler", this);
	    	Page *root = qml->createRootObject<Page>();
	    }
	    else
	    	print("Error in main QML") ;
}

void ContactBookhandler::show(){
	print("show called");
}

 Now if you see the Contact Handler constructor I get the contact.qml and set this class to its setContextProperty

But when I try to call its method show() from contact.qml actionItem onTriggered it would not work

 

Kindly help me out here I don't get what minor thing I am missing out.

 

Contributor
Posts: 10
Registered: ‎10-29-2012
My Device: Blackberry Curve 8520
My Carrier: Idea

Re: QML and C++ intergration using setContextProperty()

Was anybody able to solve this one?

Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: QML and C++ intergration using setContextProperty()

[ Edited ]

Cant really understand what the exact issue is, but you need to call setContextProperty before calling createRootObject. So in your code, the context property in the first class wouldnt be available to the qml but the order that you have it in ContactBookhandler would make that object available to the QML you just loaded as its root.

Like all of my posts
Contributor
Posts: 10
Registered: ‎10-29-2012
My Device: Blackberry Curve 8520
My Carrier: Idea

Re: QML and C++ intergration using setContextProperty()

I've main.qml in which I have a Tab bar having two tabs. 

 - Result

- My Numbers

 

Each of these tabs have a separate c++ classes to handle them, named:

- Results.hpp/cpp

- MyNumbers.hpp/cpp

 

When I create the QmlDocument in Result.hpp/cpp using:

 

resultQmlDocument = QmlDocument::create("asset:///main.qml").parent(this);
resultQmlDocument->setContextProperty("SomeKey", this);
resultRoot = resultQmlDocument->createRootObject<AbstractPane>();

 

Now I can call Q_INVOKABLE functions of Results.cpp in QML using SomeKey.funcName()

 

resultQmlDocument is public and can be accessed from MyNumbers.cpp

 

In MyNumbers.cpp I do

resultDocument->setContextProperty("SomeOtherKey", this)

 

Now when I try to use MyNumber.cpp Q_INVOKABLE functions in QML using SomeOtherKey.funcName(), I get the following error:

 

Result of expression 'SomeOtherKey.funcName' [undefined] is not a function.