PDA

View Full Version : Multiple connections to one method



davisjamesf
16th November 2007, 19:07
I have ten buttons connected to one method:

connect(m_myPushButton1, SIGNAL(clicked()), this, SLOT(MyMethod()));
connect(m_myPushButton2, SIGNAL(clicked()), this, SLOT(MyMethod()));
...
connect(m_myPushButton10, SIGNAL(clicked()), this, SLOT(MyMethod()));


Is there a way I can find out what button did the click in this method?

void TestClass::MyMethod()
{
//if button1 made the click, place a 1 in the spinner box
//if button2 make the click, place a 2 in the spinner box
// etc.
m_SpinnerBox->setValue(...find the button that made the click...);
}

marcel
16th November 2007, 19:13
You can use QObject::sender() in the slot and do a dynamic cast to QPushButton and compare the result with your ten buttons.

But it would really be nicer if you had ten slots, one for each button.

jpn
16th November 2007, 19:15
QButtonGroup is there for exactly this purpose. ;)

davisjamesf
16th November 2007, 19:21
Thanks. In reality, I have MANY more slots than my example has. All it The method has to do is take input data from a list , evaluate the data, and add it to a spinner box that corresponds the button I pressed. So for each button, there is an associated spinner box. I guess I would not only have to figure out the button I pressed, but I would have to map a spinner box with the button (perhaps a layout?)

...Just read jpn's reply after submitting this...I'll give his idea a try. Thanks

wysota
16th November 2007, 20:11
QSignalMapper can also be used, especially if you want to map spin boxes. You can then have a slot that takes a spin box pointer as its argument (just make sure it's cast to QWidget*).