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
Contributor
Posts: 14
Registered: ‎01-21-2013
My Device: BB10 alpha device
My Carrier: NA

What are the different ways by which c++ can notify cascades QML pages?

Hi

 

I am new to cascades QML. My application has User Interfaces in QML and the business logic in C++. These two components need to communicate very frequently. For this I have opened some of my c++ objects to QML using :

 

qml->setContextProperty("_app", this);

 

This allows me to access all the public slots of _app in qml pages. But the problem is when c++ needs to notify QML how does it do it?

 

Currently, I have used the following method:

I have declared a property variable say loginStatus  and an corresponding method onLoginStatusChanged() in qml. When I need to send any notification to qml I change the value of this variable in my c++ code (_app.loginStatus). This change in the value calls onLoginStatusChanged() method in qml where I take the necessary action.

 

 

QML Code :

property int loginStatus: _app.loginStatus

onLoginStatusChanged: {
        console.log("LoginScreen.qml : Login status " + loginStatus);

        //Take Action
    }

 

Is this correct? Is there any other way where in I can directly call functions in QML from C++?

Developer
Posts: 291
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: What are the different ways by which c++ can notify cascades QML pages?

QMetaObject::invokeMethod will let you call QML / javascript functions directly.

You can also define a signal in your C++ object, and then manually connect to this in QML:

C++ code:

class MyObject : public QObject
{
..... blah blah ...

signal void loginStatusChanged(int newStatus);

void dostuff()
{
emit loginStatusChanged(12345);
}
}


QML code:
onCreationCompleted: {
_app.loginStatusChanged.connect(onLoginStatusChanged);
}

function onLoginStatusChanged(newStatus) {
// take action
}

The 'signal' approach is a bit cleaner IMO - it is more "QT" -like, and it means that you can have multiple parts of your QML connected to the signal, and you don't have to keep track of them from C++. If you try to call the QML directly you introduce a dependency that is a little harder to keep track of.
Developer
Posts: 16,750
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: What are the different ways by which c++ can notify cascades QML pages?

just to add:
you can also define a Q_PROPERTY with the NOTIFY keyword, this automatically emits a signal whenever the property is changed.
see
http://doc.qt.digia.com/4.7/properties.html
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 14
Registered: ‎01-21-2013
My Device: BB10 alpha device
My Carrier: NA

Re: What are the different ways by which c++ can notify cascades QML pages?

Thanks for Reply. I will try the connecting signals from c++ classes to qml screens approach.