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
Regular Contributor
Posts: 52
Registered: ‎02-08-2013
My Device: Z10
My Carrier: Rogers
Accepted Solution

connect to a signal of a QObject returned from C++

Hi all,

 

In C++,

 

Class A : public QObject

{

Q_SIGNALS:

void result( B* ret);

}

 

Class B: public QObject

{

Q_SIGNALS:

void result( C* ret);

}

 

Class C : public QObject

 

on QML side

attachedObject: [

    A {

      onResult: {

         ret.request( ); // Question: how to connect to B' signal?

      } 

    }

]

 

 

Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: connect to a signal of a QObject returned from C++

[ Edited ]

There doesn't seem to be an easy way to do this type of connection.

 

You could use setProperty() on the c++ side to pass in the reference to the c++ object to the QML. Then in QML in the onCreationComplete function, you could do the connection by accessing the property. This is not what you asked for because it makes the connection in QML, but it works. I never use this approach because it doesn't hide the implementation from the view which violates my principles Smiley Happy (I use QML for the view and c++ for the controller).

 

The easiest way to solve this is probably not to use connections at all. At the spot in your c++ object where you would emit the signal, just use QMetaObject::invokeMethod(qmlObject, "functionName", ...). This will invoke the function but has a few downsides that signals and slots don't have.

 

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

Re: connect to a signal of a QObject returned from C++

ret.signalName.connect ( onBResult );

function onBResult(ret) {

}


You'll need to play around with Q_DECLARE_METATYPE and qmlRegisterType to ensure that B#'s methods are accessible to QML

// in your headerfile for class B
typedef B * BStar;
Q_DECLARE_METATYPE(BStar);

// in your startup:
qmlRegisterType<B>();
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: connect to a signal of a QObject returned from C++

Hi!

Something like this should work. Untested code to show the idea:

// This is top-level QML item

property variant b; function disconnectSignals() { if (b) b.result.disconnect(onBResult) }
function onBResult(ret)
{
// ...
}
attachedObject: [ A { onResult: { if (!b) { b = var; b.result.connect(onBResult) } b.request( ); } } ]

Call disconnectSignals() function before destroying the QML. Signals connected using connect() aren't automatically disconnected.

 

Store the pointer in 'property variant' and check if it already exists to avoid connecting it twice.

 

'var' is not a good name for variable and can conflict with var QML type.


Andrey Fidrya, @zmeyc on twitter
Regular Contributor
Posts: 52
Registered: ‎02-08-2013
My Device: Z10
My Carrier: Rogers

Re: connect to a signal of a QObject returned from C++

I could get signal object C from B by using this way.  only issue is when I access C's property (QObject D), it return null. Continue on investigation.

Regular Contributor
Posts: 52
Registered: ‎02-08-2013
My Device: Z10
My Carrier: Rogers

Re: connect to a signal of a QObject returned from C++

update: I could get signal of B on QML side.  The signal returned object is C (QObject derived object).  I couldn't rerive any property of return object C.

 

If I added a method in B, e.g. C* getResult( )

 

function _onBsignal( cObject)

{

// not working

cObject.PropertyD;

 

//working 

var c = B.getResult( );

c.PropertyD;

}

 

Basically, simple signal works, but not working with signal with Q-derived object as parameter.

Regular Contributor
Posts: 52
Registered: ‎02-08-2013
My Device: Z10
My Carrier: Rogers

Re: connect to a signal of a QObject returned from C++

found a solutoin:

 

using Qt Quick's Connection