0

I'm creating multiple (undefined at design time) number of buttons programatically. How can I determine which button was clicked in my handler?

for (int i = 0; i < XXX; i++) {
    Button *btn = Button::create();
    QObject::connect(btn, SIGNAL(clicked()), this, SLOT(onButtonClicked()));
    ...
}

void MyClass::onButtonClicked() {
    ???? Which button ???
}

On all platforms I'v previously used (Borland.VCL, Cocoa, Cocoa Touch, WinRT/PRT, Android...) event handler always(!!!) has a sender parameter indicating the object instance invoked the event.

So, how to do it in BlackBerry Cascades?

PS. Pleas don't tell me I have to create my own Subclass of Button, add a SIGNAL onClicked(MyButton *sender) and propagate it... This will end my BlackBerry development in it's early beginning. :)

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44

1 Answers1

0

A very similar question has been answered in the standard BB forums here:

http://supportforums.blackberry.com/t5/Native-Development/Tracking-Signal-Sender/m-p/1969275#M5539

So you don't have to go there, here is the question and the text of the useful responses:

I have an array multiple buttons created and assigned to specific function. But I would like to know which button the user pressed. Any idea? How should I implement the Signal and Slots

for(int i=0;i<8;i++)
{
button[i]= Button::create();
buttonContainer->add(button[i]);
QObject::connect(button[i], SIGNAL(clicked()), this, SLOT(doStuffs()));
}

1) Use QObject::sender() in your slot and cast the result to Button. If you first do "button[i].tag = i" then you can find out in the slot exactly which button was triggered. Unfortunately, you first need to subclass Button and include an integer tag value. I've already filed a feature request about this.

2) You also may want to look up QSignalMapper:

http://qt-project.org/doc/qt-4.8/qsignalmapper.htm​l

It's a utility class that's part of Qt, which is designed to help with this exact situation.

Peter Strange
  • 2,640
  • 11
  • 11
  • Hi, Peter. Thank you very much, that's exactly what I was looking for. But IMHO, any handled worth having *sender parameter. May be you may also fill a feature request for this? – Grigoriy Uskov Sep 19 '13 at 14:34