PDA

View Full Version : SLOT and QPushButton



mickey
12th February 2006, 17:35
Hi,
I' have connect (from Designer) two QPushButton to the same SLOT; and within the SLOT I'd like to know wich button called the SLOT. Is it possible?

Cesar
12th February 2006, 17:40
void mySlot() {
QPushButton * b = qobject_cast<QPushButton *>(sender());
if (b) {
if (b == button1) {
//button1 SIGNAL()ed
} else if (b == button2) {
//button2 SIGNAL()ed
}
}
}

yop
12th February 2006, 17:42
Hi,
I' have connect (from Designer) two QPushButton to the same SLOT; and within the SLOT I'd like to know wich button called the SLOT. Is it possible?
You can use QObject::sender() (http://doc.trolltech.com/3.3/qobject.html#sender) and cast to a pushbutton. A better (for me that is) alternative would be to use a QButtonGroup (http://doc.trolltech.com/3.3/qbuttongroup.html). I believe I've had the same discussion with someone else in the forums ;)

EDIT:
@Cesar: qobject_cast AFAIK is not available for Qt 3.x (and mickey seems to be using Qt3 on Windows)

mickey
13th February 2006, 11:08
I solved so:

QPushButton* button = (QPushButton*)(sender());

but "qobject_cast" what is it? I don't find it!
Thanks

Everall
13th February 2006, 11:24
but "qobject_cast" what is it? I don't find it!

in previous post from Yop :


EDIT:
@Cesar: qobject_cast AFAIK is not available for Qt 3.x (and mickey seems to be using Qt3 on Windows)

You'll find it in QT4 docs : QObject class

Cesar
13th February 2006, 11:29
@Cesar: qobject_cast AFAIK is not available for Qt 3.x (and mickey seems to be using Qt3 on Windows)
Sorry, guys :( I haven't bother to take a look at QT3 docs to make sure qobject_cast<> is available in QT3...

wysota
13th February 2006, 12:00
You can use dynamic_cast instead. The sender has to be a QObject derived class, so dynamic_cast should work the same (if the compiler supports rtti of course).

meissner
13th February 2006, 22:19
Hi there,

I don't use the Designer, but have the same problem. Merely I don't want to write for every QButton a connect line. So I decide to use a QButtonGroup. But I dont know, which Signal I must use. Here is the Code:


buttons = new QButtonGroup();

buttons->addButton(einer1, 1);
buttons->addButton(zweier1, 2);
buttons->addButton(dreier1, 3);
...blabla

connect(buttons, SIGNAL(???), this, SLOT(createMenus()));

And how can I use the button in the Slot createMenus()?

Someone told me that i also can use a signalmapper. What about this possibility?

Thanks

jacek
13th February 2006, 22:48
But I dont know, which Signal I must use.
Try this:
connect( buttons, SIGNAL( clicked( int ) ), this, SLOT( createMenus() ) );

yop
13th February 2006, 22:52
Try this:
connect( buttons, SIGNAL( clicked( int ) ), this, SLOT( createMenus() ) );
The signal has an int argument, the slot has none is that legal?

jacek
13th February 2006, 22:56
The signal has an int argument, the slot has none is that legal?
Of course it is:

From Qt docs (http://doc.trolltech.com/4.1/signalsandslots.html):
The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.)

yop
13th February 2006, 23:14
Of course it is: ...some rtfm
I would swear that it wasn't but since it was too obvious to miss, I thought I'd better ask and I'm glad I did. Thanks...

wysota
13th February 2006, 23:29
I would swear that it wasn't but since it was too obvious to miss, I thought I'd better ask and I'm glad I did. Thanks...

Slot are generaly invoked as function calls. If a slot has less parameters than the signal (provided that only the last arguments of the signals are missing) it is possible to tell, which parameters of the signal are mapped to which parameters of the slot. That's why it is possible for a slot to have less parameters than the signal connected to it. The situation is exactly the same as with default arguments for functions.

meissner
14th February 2006, 16:49
Ok, it works.

But how can I get the id from the QButton of the QButtonGroup that was clicked to use it in my SLOT?

For example:


QPushButton* cbutton = (QPushButton*)(sender());
buttons->button( buttons->id( cbutton ) )->setText("bla");
This code produce an Segmentation fault and I guess the sender() funktion is stupid in this context, isn't it?

wysota
14th February 2006, 16:51
The "int" value of QButtonGroup::buttonClicked signal carries it.

Chicken Blood Machine
15th February 2006, 07:46
Ok, it works.

But how can I get the id from the QButton of the QButtonGroup that was clicked to use it in my SLOT?

For example:


QPushButton* cbutton = (QPushButton*)(sender());
buttons->button( buttons->id( cbutton ) )->setText("bla");
This code produce an Segmentation fault and I guess the sender() funktion is stupid in this context, isn't it?

Yes in fact, that is one of the dangers of using sender() - you break the loose coupling and need to know exactly what kind of widget invoked the slot (of course using a C++-style dynamic_cast<> instead of (QPushButton*) as advised earlier would have trapped the error).

Change the signature of your function to take an int parameter. This will contain the id of the button that was pressed. Then you can do:



buttons->button(id)->setText("bla");