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?
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?
Regards
Qt Code:
void mySlot() { if (b) { if (b == button1) { //button1 SIGNAL()ed } else if (b == button2) { //button2 SIGNAL()ed } } }To copy to clipboard, switch view to plain text mode
You can use QObject::sender() and cast to a pushbutton. A better (for me that is) alternative would be to use a QButtonGroup. I believe I've had the same discussion with someone else in the forumsOriginally Posted by mickey
EDIT:
@Cesar: qobject_cast AFAIK is not available for Qt 3.x (and mickey seems to be using Qt3 on Windows)
Last edited by yop; 12th February 2006 at 16:47.
I solved so:
Qt Code:
To copy to clipboard, switch view to plain text mode
but "qobject_cast" what is it? I don't find it!
Thanks
Regards
in previous post from Yop :but "qobject_cast" what is it? I don't find it!
You'll find it in QT4 docs : QObject classEDIT:
@Cesar: qobject_cast AFAIK is not available for Qt 3.x (and mickey seems to be using Qt3 on Windows)
Last edited by Everall; 13th February 2006 at 10:30.
Sorry, guys I haven't bother to take a look at QT3 docs to make sure qobject_cast<> is available in QT3...Originally Posted by yop
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).
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:
Qt Code:
buttons->addButton(einer1, 1); buttons->addButton(zweier1, 2); buttons->addButton(dreier1, 3); ...blabla connect(buttons, SIGNAL(???), this, SLOT(createMenus()));To copy to clipboard, switch view to plain text mode
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
Try this:Originally Posted by meissnerQt Code:
connect( buttons, SIGNAL( clicked( int ) ), this, SLOT( createMenus() ) );To copy to clipboard, switch view to plain text mode
The signal has an int argument, the slot has none is that legal?Originally Posted by jacek
Of course it is:Originally Posted by yop
From Qt docs:
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.)
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...Originally Posted by jacek
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.Originally Posted by yop
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:
This code produce an Segmentation fault and I guess the sender() funktion is stupid in this context, isn't it?Qt Code:
buttons->button( buttons->id( cbutton ) )->setText("bla");To copy to clipboard, switch view to plain text mode
The "int" value of QButtonGroup::buttonClicked signal carries 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).Originally Posted by meissner
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:
Qt Code:
buttons->button(id)->setText("bla");To copy to clipboard, switch view to plain text mode
Save yourself some pain. Learn C++ before learning Qt.
Bookmarks