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: 75
Registered: ‎06-14-2013
My Device: Z10
My Carrier: PosteMobile

How to identify a graphical object among identical objects of the same class

I have many graphical objects of type Image *.

This objects are used as image for associated ImageButton. The Image objects are stored in a QList. I need to determine what object between the objects contained in the QList has been clicked.

 

I can get the ImageButton object which hase been clicked but i can't figure out how to find what is the respective object in the QList. I have tried to get the Image object with the defaultImage() method and use the boolean operator ==  to compare it with my Image * object:

 

                    if(CardImage == &(TouchedCard.defaultImage() ) )

 

but i get error at compile time (taking address of temporary). The only thing i can do it's to compare the source:

 

                    TouchedCardImage = TouchedCard->defaultImage();

                    if(CardImage->source() == TouchedCardImage.source() )

but it's possible that two ImageButton have the same source so this cannot be used to identify the object.

 

So is there a way to do it without memorizing each ImageButton object pointers ?

Thanks in advance

Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: How to identify a graphical object among identical objects of the same class

Its not clear exactly what it would be but there seems to be a much better way to accomplish what you are trying to do. How are you displaying the image buttons and adding the tap handlers? What is TouchedCard? Its very fragile to try and test Image pointers for equality, ImageButton could be doing some copying underneath the covers. Is there something else you could test on? Like list index?
Like all of my posts
Regular Contributor
Posts: 75
Registered: ‎06-14-2013
My Device: Z10
My Carrier: PosteMobile

Re: How to identify a graphical object among identical objects of the same class

Thanks kylefowler.

I'm sorry I must haven't been very clever.

 

The variables showed in the previous code are declares as below:

 

    Image *CardImage;

    Image TouchedCardImage;
    ImageButton *TouchedCard;

 

The TouchedCard is the QObject which send the clicked() signal and I get it in this way:

TouchedCard = (ImageButton *) QObject::sender();

 

So when a card is clicked signal is triggered the slot method is called and the Object who send this signal is identified as shown above.

 

Answering to your questions:

 

1- How are you displaying the image buttons and adding the tap handlers?

 

The images are added to a Container object and then the Container is added in turn to the Page.

The handler of the clicked() signal is the same slot method for each card and is here where the clicked image must be identified.

 

2- Is there something else you could test on? Like list index?

 

The problem is that the handler method or more precisely the slot method only know what is the object which send the clicked() signal but this object has no ID or something to distinguish it from an identical card with the same image. A usefull method would be something like getId() which return a unique identifier of the object that is what I am looking for so far.

 

I hope this time I cleared your mind.

 

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

Re: How to identify a graphical object among identical objects of the same class

[ Edited ]

i would suggest to use an additional data structure to handle that, for example a QHash that stores the identifier of the ImageView (objectname or a custom property) with the associated pointer.

 

you can set any property you want on QObjects (in qml or c++), so identifying your class should not be an issue.

----------------------------------------------------------
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
Retired
Posts: 418
Registered: ‎07-18-2012
My Device: Q10
My Carrier: Bell Canada

Re: How to identify a graphical object among identical objects of the same class

Hi,

 

An alternate to Simon's good suggestion is to create another object that isn't tracked, but tracks state.

 

You connect to it - not to your GUI classes - here's what I mean - but using a Button... 

class ClickProxy : public QObject {
public:
	ClickProxy(bb::cascades::Button * owner, int which) : QObject(owner) , _which(which) {
		connect(owner,SIGNAL(clicked()),this,SLOT(onClicked()));
	}
	virtual ~ClickProxy() {
	}

	Q_SLOT void onClicked() {
		emit clicked(_which);
	}
signals:
	void clicked(int);
private:
	int _which;
};

 

 

 

 

 

--
Rob is no longer associated with BlackBerry.