PDA

View Full Version : Re-casting SIGNAL's and/or SLOT's



Wasabi
7th October 2010, 21:56
I've looked through the webs for an answer to this question which I'm sure has been answered a few dozen times before, but I can't find anything regarding it, so...

How can I re-cast a SIGNAL or SLOT? For instance, I have a QCheckBox which I want to connect to a QLineEdit so that if the Box is unchecked, the LineEdit is disabled.

My first idea was of course to use:

connect(box,SIGNAL(stateChanged(int)),LineEdit,set Enabled(bool)));

Since stateChanged emits a 0 if unchecked and a 2 if checked (1 if partially checked, which isn't relevant in my case), that'd work well.

However, as I was already aware, the SIGNAL and SLOT need to have the same arguments. I was hoping that since int can be implicitly cast to a bool (0 == false, everything else == true), it'd work, but it doesn't.

So now I'm stuck. Any hints?

wysota
7th October 2010, 22:07
You can't do such a cast, signatures have to match character to character. You need a custom slot with matching signature and call the proper slot you want from there.

Wasabi
8th October 2010, 02:26
Grrr...

Is there some other way of doing what I want to do, though? Using checkboxes to enable/disable other widgets must be a pretty common occurrence.

ChrisW67
8th October 2010, 03:27
The general approach is as Wysota describes. Depending on your UI you may consider a checkable QGroupBox.

wysota
8th October 2010, 11:14
QCheckBox is an abstract button so it has a toggled(bool) signal that will work nicely in your case.

jamo_
1st May 2011, 01:42
Hi,

I was having this issue but managed to sort it by using the clicked checkbox signal rather than statechanged:


connect(checkBox, SIGNAL(clicked(bool)), otherWidget, SLOT(setEnabled(bool)));

I had missed it as it is one of the inherited signals from QAbstractButton ..

Thanks,

:-)