Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect signals to slots with constant values

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?

like image 672
Leif Andersen Avatar asked Mar 25 '12 16:03

Leif Andersen


People also ask

How do I connect my signal to my slot QT?

To connect the signal to the slot, we use QObject::connect(). There are several ways to connect signal and slots. The first is to use function pointers: connect(sender, &QObject::destroyed, this, &MyObject::objectDestroyed);

In what order will the slots be executed if they are connected to one signal?

if several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted.

Are Qt signals and slots thread safe?

It is generally unsafe to provide slots in your QThread subclass, unless you protect the member variables with a mutex. On the other hand, you can safely emit signals from your QThread::run() implementation, because signal emission is thread-safe.


2 Answers

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.

like image 198
BiTOk Avatar answered Nov 07 '22 07:11

BiTOk


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.

like image 21
Lol4t0 Avatar answered Nov 07 '22 06:11

Lol4t0