Connect signals to slots with constant values

Leif Andersen picture Leif Andersen · Mar 25, 2012 · Viewed 7.8k times · Source

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?

Answer

BiTOk picture BiTOk · Dec 17, 2015

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.