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: 23
Registered: ‎11-15-2012
My Device: Red Z10 & PlayBook
My Carrier: E-Plus
Accepted Solution

QtQuick Connections element - C++ signals in QML

Hello everyone Smiley Happy

 

QtQuick has a Connections element to access C++ signals in QML and handle them. This looks like this:

 

Connections {
        target: live
        onLiveUpdateStarted: {
            console.debug("qml onLiveUpdateStarted")
            updatingLiveData.running = true
            updatingLiveData.visible = true
        }
        onLiveUpdateFinished: {
            console.debug("qml onLiveUpdateFinished")
            updatingLiveData.running = false
            updatingLiveData.visible = false
        }
}

 

where "live" is set through main.cpp: viewer.rootContext()->setContextProperty("live", live);

 

How do I do this connection with Cascades as Connections doesn't seem to be available.

 

Best Regards,

Sebastian

Highlighted
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: QtQuick Connections element - C++ signals in QML

Actually the Connections element is still available, but you have to "import QtQuick 1.0" first to get it into the namespace.  At that point, because it's not a visual node, you'll also need to place it in an attachedObjects list instead of just out with the rest of the page content as you could in Qt QML.

 

You may not need it, however. Another option that doesn't require bringing in QtQuick is to make direct connections to the signals, something like this:

onCreationCompleted: {
    live.liveUpdateStarted.connect(someFunction);
    live.liveUpdateFinished.connect(someOtherFunction);
}

Then you'd obviously define regular JavaScript functions (e.g. someFunction) with the same content as the handlers you showed.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Contributor
Posts: 23
Registered: ‎11-15-2012
My Device: Red Z10 & PlayBook
My Carrier: E-Plus

Re: QtQuick Connections element - C++ signals in QML

Thanks, that's even easier than with Connections.

 

How could I have found it in any documentation? Cause this is the most annoying thing for me in Cascades at the moment. QtQuick/Qt Creator has this wonderful QtAssistant, but I couldn't find anything like that here. Only the online (!) API reference, but the navigation is hard there.

 

Did I missed something?

 

Best Regards,

Sebastian

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: QtQuick Connections element - C++ signals in QML

If you looked only at the APIs, you'd be overlooking lots of the "big picture" stuff, of which this page is one tiny fraction: https://developer.blackberry.com/cascades/documentation/dev/signals_slots/index.html (using QML to connect stuff is covered near the bottom). And I don't think the part about still using Connections is documented in these pages at all, perhaps, so it's just something we (somebody, I forget who) pieced together and told the rest of us, probably in these forums.

Yes, it's imperfect, but we're working with a rapidly evolving platform and unfinished SDKs, so it needs some extra effort on our behalf in the interim.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 54
Registered: ‎05-23-2012
My Device: Developer
My Carrier: Developer

Re: QtQuick Connections element - C++ signals in QML

From the docs:

"By default, a signal is emitted for every connection you make; two signals are emitted for duplicate connections."

 

 

The problem I'm having is that calling connect() in QML is causing the connections to stack, but I can't see a way in QML to clear the connections or ensure there is only one e.g. from the Cascades docs:

 

Button {
    id: increaseButton
    text: "Increase the value"
    onClicked: {
        cppObject.valueChanged.connect 
                (increaseButton.onCppValueChanged);
        cppObject.value = cppObject.value + 1;
    }
    function onCppValueChanged (val) {
        increaseButton.text = "Set value to " + (val + 1);
    }
}

 

The second time you click the button onCppValueChanged will actually be connected twice and will be called twice. Is there a way in QML to do something like:

 

cppObject.mySignal.connect(myFunc, Qt::UniqueConnection);

 

I tried this but it didn't work:

 

cppObject.mySignal.disconnect(myFunc);

cppObject.mySignal.connect(myFunc);

 

 

----
I know exactly where the problem *might* be.
Developer
Posts: 54
Registered: ‎05-23-2012
My Device: Developer
My Carrier: Developer

Re: QtQuick Connections element - C++ signals in QML

I ended up solving my problem here: http://supportforums.blackberry.com/t5/Cascades-Development/qml-function-is-executing-more-no-of-tim...

----
I know exactly where the problem *might* be.