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
Developer
Posts: 580
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA
Accepted Solution

Getting Object Associated With Event

I'd like to dynamically create some UI objects and connect their Tap event to a slot.

 

The event that gets passed to the slot is TapEvent:

 

https://developer.blackberry.com/native/reference/cascades/bb__cascades__tapevent.html

 

How can I use the event (or something else) to determine which UI object was tapped?

 

Thanks,
Daniel

Developer
Posts: 16,786
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Getting Object Associated With Event

in c++ you can use sender() to get the sender of the event
----------------------------------------------------------
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
Developer
Posts: 580
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA

Re: Getting Object Associated With Event

Interesting. Unfortunately that seems to return an uninitialized QObject when used in the tap event handler. All of the code examles I have found on these forums using sender() seem to use it in the context of a QNetworkRequest.

Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: Getting Object Associated With Event

You should be able to cast that QObject to the object type that sent the signal. Check out the paymentservice sample:

https://github.com/blackberry/Cascades-Samples/blob/master/paymentservice/src/PaymentServiceControl....

 

Or, if you don't want to go as far as casting, you can still read out the various proprties to find a differentiator (label etc) to know which object sent the signal.

Garett
@garettBeuk
--
Goodbye everybody!
Developer
Posts: 580
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA

Re: Getting Object Associated With Event

Hi Garett,

 

I am indeed casting, as follows:

 

Container* obj = qobject_cast<Container*>(sender());

 

But 'obj' is bsically null / uninitialized.

 

I wonder if this technique works with touch gestures?  (I have no idea why setting up touch gesture handlers is different than setting up normal signals and slots, but maybe this uses an approach that makes sender() not work?)

Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: Getting Object Associated With Event

AFAIK it should be a function that works with all QObjects. Are you trying to read a specific property to know that it's null? Or is it showing as a null object?

Garett
@garettBeuk
--
Goodbye everybody!
Developer
Posts: 16,786
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Getting Object Associated With Event

check if sender() returns null or if only your cast fails.
qobject_cast returns 0 if the object is of a different type!
----------------------------------------------------------
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
Developer
Posts: 580
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA

Re: Getting Object Associated With Event

Hi guys,

 

Good thought simon, here's what I get if I qDebug() the sender() return value without the cast:

 

bb::cascades::TapHandler(0x8863550)

 

So it looks like the sender is the TapHandler, rather than the UI control, when gesture handlers are used. Bummer.

 

I wonder if the TapHandler holds a reference to the UI object that I could grab?

Developer
Posts: 580
Registered: ‎04-01-2009
My Device: Z10, PlayBook
My Carrier: NA

Re: Getting Object Associated With Event

Ok, solved it by trial and error.

 

Looks like the parent of the TapHandler is the Container. So here's the final solution:

 

TapHandler* tapHandler = qobject_cast<TapHandler*>(sender());
Container* container = tapHandler->parent();

 

Thanks for your help guys.