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
Highlighted
Developer
Posts: 24
Registered: ‎02-09-2014
My Device: BlackBerry Z30
My Carrier: Telkom SA
Accepted Solution

Expose c++ class to children of a NavigationPane

Hi all,

I have C++ classes that work perfectly well on the first page of my application. Now I would like to push some pages on the stack and still be able to use the same classes. How do I do that ? Thanks in advance.

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Expose c++ class to children of a NavigationPane

You could attach them to your navigationPane (if using one) give it an ID then access it through the navigationPane ID.

 

Alternatively you can declare something as global using the Qt declaration.

 

Qt.myClass = _myClass;   // Make C++ class global

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 24
Registered: ‎02-09-2014
My Device: BlackBerry Z30
My Carrier: Telkom SA

Re: Expose c++ class to children of a NavigationPane

Thanks for your reply. Where do I type that code? in main.cpp?
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Expose c++ class to children of a NavigationPane

[ Edited ]

The Qt line will go in the first qml file usually in something like an onCreationCreated slot, i.e.

 

    onCreationCompleted: {
        Qt.app = _app;			// Set app as a global QML variable

        _app.signalTest.connect(writeToConsole);
    }

 Obviously you will need to pass your classes through to QML in the applicationui.cpp, something like,

 

    QmlDocument *qml = QmlDocument::create("asset:///main.qml")
		.parent(this)
		.property("_app", this)
		.property("_info", info)
		.property("_dirPaths", dirPaths);

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Retired
Posts: 418
Registered: ‎07-18-2012
My Device: Q10
My Carrier: Bell Canada

Re: Expose c++ class to children of a NavigationPane

Can make a property global from c++ via:

QmlDocument::defaultDeclarativeEngine()->rootContext()->setContextProperty("fred",x);

 

--
Rob is no longer associated with BlackBerry.
Developer
Posts: 24
Registered: ‎02-09-2014
My Device: BlackBerry Z30
My Carrier: Telkom SA

Re: Expose c++ class to children of a NavigationPane

Hi all, thanks for your answers. So here is what worked for me:

QmlDocument qml = QmlDocument::create("assets:///main.qml");
qml->setContextProperty("_app",this);

The same like in the documentation... I don't know why it didn't work before.

Thanks again for your answers.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Expose c++ class to children of a NavigationPane

Glad it worked for you.

I'll just point out this is the same answer as the one I gave above just that I used the builder functions which I find easier to read when coding.

 

So, either use .property() or setContextProperty() depending on your own preference as they are one and the same.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.