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
Contributor
Posts: 12
Registered: ‎06-01-2011
My Device: Blackberry Tourch
My Carrier: 3 mobile
Accepted Solution

How to respond to application status changes

Hi Folks,

 

I wondered if anyone had some general advice on how I could implement the following.

 

My application needs to let the GUI know what state it is in so that the GUI can respond to those status changes.

 

The status changes are not triggered by the user but by other factors.

 

The idea I had was to have an integer variable that gets set in the gui when the application changes state.  I could then implement a timer in the gui which checks the variable and reacts as required.

 

I then got thinking, is there a way I can emit a signal from my main application logic and have a javascript function called/triggered?  This would eliminate the need to have a timer and would make the gui more responsive to status changes.  But I am not sure how I could implement this, if it is possible.

 

I have looked through the examples but they all seem to respond to some user action, where as I need something to respond if/when a certain condition arrises.

 

Does anyone have any suggestions on how to tackle this?

 

Thanks in advance.

Highlighted
Contributor
Posts: 12
Registered: ‎06-01-2011
My Device: Blackberry Tourch
My Carrier: 3 mobile

Re: How to respond to application status changes

Managed to figure this out.

 

QObject::connect(this, SIGNAL(mySignal()), root, SLOT(mySlot()));

 

It makes sense now, havn't quite grasped this signal->slot business, but i'm getting there slowly.

Retired
Posts: 329
Registered: ‎05-07-2012
My Device: BB Alpha
My Carrier: N/A

Re: How to respond to application status changes

Hi there,

 

Indeed Signals & Slots are a very powerful component of Qt. If you need more information and haven't checked this out yet, you can read up more here:

http://qt-project.org/doc/qt-4.8/signalsandslots.html

 

If more clarification is needed do not hesitate to post again.

 

Good luck!

 

Martin

 

Contributor
Posts: 30
Registered: ‎05-24-2012
My Device: Developer
My Carrier: Developer

Re: How to respond to application status changes

Hi

 

This would be very simple to do with QtQuick but Cascades lacks some of the QML-elements that makes life easier.

 

Anyway, probably what you could do is something in style with the following:

 

Create a QObject-derivate class and add a property named, for example, 'appState'.

 

class AppState: public QObject
{
    Q_OBJECT
    Q_PROPERTY(int appState READ appState WRITE setAppState NOTIFY appStateChanged)
public:
    // Add Constructor which takes a QObject as parent

// Add getter/setter and signal int appState() const { return m_state; }
void setAppState(int state) { if (m_state != state) { m_state = state; emit appStateChanged(); } signals: void appStateChanged(); private: int m_state; }

 

In the Application class (the one that loads your QML-document) add following:

AppState *state = new AppState(this);
qml->setContextProperty("appState", state);

 

 

Add following to the root element in QML: (probably a Page component):

Page {
    ...

    property int uiState: appState.appState

    onUiStateChanged: {
        // Execute your JavaScript here
    }

    ...
}

 

 

This should probably work. What happens is that each time you set a new state with the 'setAppState' method you emit the appStateChanged-signal. QML will bind to the 'appState' property and receive the notification when the property is changed and the uiState property will changed which in turn emits a 'onUiStateChanged' signal Smiley Happy.

Contributor
Posts: 12
Registered: ‎06-01-2011
My Device: Blackberry Tourch
My Carrier: 3 mobile

Re: How to respond to application status changes

Hi,

 

Thanks for the responses.

 

@mgoulet - Thanks for the link.  I hadn't come across that information so I'll have a read through.

 

@marioboikov - Thanks for the example code.  It looks like a tidy way to implement what I wanted.

 

I'm glad I posted, it seems things are starting to SLOT() in to place