02-27-2014 07:39 PM
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:
How can I use the event (or something else) to determine which UI object was tapped?
Solved! Go to Solution.
02-28-2014 03:03 AM
02-28-2014 09:38 AM
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.
02-28-2014 10:16 AM
You should be able to cast that QObject to the object type that sent the signal. Check out the paymentservice sample:
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.
02-28-2014 10:21 AM
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?)
02-28-2014 10:28 AM
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?
02-28-2014 10:28 AM
02-28-2014 10:46 AM
Good thought simon, here's what I get if I qDebug() the sender() return value without the cast:
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?
02-28-2014 10:51 AM
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.