PDA

View Full Version : Can I pass a default value to a slot?



ricardo
8th January 2010, 13:05
Hi friends!

I know the arguments of signals and slots are types, not variables, but is there any way to do something like this?

connect(ui.pushButtonResetZoom, SIGNAL(pressed()), ui.doubleSpinBoxZoomFactor, SLOT(setValue("1.0")));

(see last argument, 1.0)

Thanks a lot.

vcernobai
8th January 2010, 14:32
You can try QSignalMapper. This way you can arrange to pass values with pressed() signal that your pushButtonResetZoom sends.

spud
8th January 2010, 14:59
You can define the function as:


public slots:
void setValue(QString value ="1.0");

this will actually create two slots.
You can then connnect with the line


public slots:
connect(ui.pushButtonResetZoom, SIGNAL(pressed()), ui.doubleSpinBoxZoomFactor, SLOT(setValue()));

ricardo
8th January 2010, 15:48
Thanks spud , I already know that, the problem is that setValue belongs to a QDoubleSpinBox (btw, parameter is a double, change "1.0" by 1.0)

Any idea?

Thanks.

Coises
8th January 2010, 15:53
I’m guessing that both the signal and the slot come from existing classes, so that it is inconvenient to re-implement them. If (as I’m also guessing) the slot is QDoubleSpinBox::setValue (http://doc.trolltech.com/latest/qdoublespinbox.html#value-prop), which takes a double, then QSignalMapper probably won’t help since double is not among the types to which it can map.

Probably the simplest way is to create a new signal and slot combination:

signals:
void setZoomFactor(double);
public slots:
void resetZoomFactor() {signal setZoomFactor(1.0);}
in any QObject you’re already sub-classing that lives in the GUI thread and is guaranteed to exist during the time you’ll need the signal. Then connect in the obvious way, using this object as a bridge.

If ui.doubleSpinBoxZoomFactor is directly accessible from the QObject you use to tie the two widgets together (and it probably will be, because you’ll probably use the main window) you can eliminate the setZoomFactor signal and just call ui.doubleSpinBoxZoomFactor.setValue in the resetZoomFactor slot.