15
votes

To connect signals to slots, as far as I know, the parameters of the signal need to match the parameters of the slot. So for example:

connect(dockWidget->titleBarWidget(), SIGNAL(closeButtonClicked()), ui->sideControls, SLOT(closeDockWidget()));

But what if I want to have a signal call a slot that has a different amount of parameters, but always pass a constant value into the slot. For example, using the above piece of code:

connect(dockWidget->titleBarWidget(), SIGNAL(closeButtonClicked()), ui->sideControls, SLOT(setDockWidget(false)));

Or in other words, whenever the button is pressed, it calls the setDockWidget() function with the false parameter. Is this possible?

3

3 Answers

17
votes

You can use lambda with desired call with constant argument. Example:

connect(obj, &ObjType::signalName, [this]() { desiredCall(constantArgument); });

More about new connect syntax: https://wiki.qt.io/New_Signal_Slot_Syntax.

9
votes

No, it is not possible. You are only allowed to connect slots with less or equal argument count, than in corresponding signal. (see documentation)

You have to create proxy slot, that will call desired one.

6
votes

In a way, yes, you can. But it's not very powerful : just declare the setDockWidget this way :

[virtual] void setDockWidget(bool state=false) ;

And declare the connection this way :

connect(emitter, SIGNAL(closeButtonClicked()), receiver, SLOT(setDockWidget()));

setDockWidget called without arguments take the default ones.