View Full Version : Signal parameter - needed?

18th October 2018, 03:09
I have the signals from a pushButton and a checkBox both connected to the same slot:

connect(pushButton_redraw_dist_plots, SIGNAL(clicked()), this, SLOT(drawDistPlots()));
connect(cbox_USE_LOGNORMAL_DIST, SIGNAL(stateChanged()), this, SLOT(drawDistPlots()));

The pushButton signal does invoke drawDistPlots(), but the checkBox signal does not. I know that clicked() takes a bool argument, and stateChanged() takes an int argument, but I'm not sure if the arguments are needed. From what I observe, it seems that the clicked() signal functions without the argument, but apparently the stateChanged() signal does not. This seems odd. I am omitting the arguments because I don't know how to make drawDistPlots() take either a bool or an int argument. Is there a way to do what I want?


I decided to try making drawDistPlots() take a dummy int argument, then use these connect statements:

connect(pushButton_redraw_dist_plots, SIGNAL(clicked(bool)), this, SLOT(drawDistPlots(int)));
connect(cbox_USE_LOGNORMAL_DIST, SIGNAL(stateChanged(int)), this, SLOT(drawDistPlots(int)));

This compiles (to my surprise), but the pushButton signal is not received.

18th October 2018, 08:05
Hi, you need to add the parameters in the signal when you call connect, but you can omit them in the slot, so you don't need a dummy parameter.
I'm not sure about connecting a signal with a bool parameter to a slot with an int, but I think it should print an error at runtime.
This should work:

connect(pushButton_redraw_dist_plots, SIGNAL(clicked()), this, SLOT(drawDistPlots()));
connect(cbox_USE_LOGNORMAL_DIST, SIGNAL(stateChanged(int)), this, SLOT(drawDistPlots()));


edit: you could also use the new Qt5 syntax: https://wiki.qt.io/New_Signal_Slot_Syntax

18th October 2018, 13:48
Thanks. After some testing, I found that this works:

connect(pushButton_redraw_dist_plots, SIGNAL(clicked(bool)), this, SLOT(drawDistPlots(bool)));
connect(cbox_USE_LOGNORMAL_DIST, SIGNAL(toggled(bool)), this, SLOT(drawDistPlots(bool)));

From what you say I could omit the dummy parameter in the slot.

26th October 2018, 00:20
A little late, but I was on vacation and couldn't log into the forum and the "forgot password" link still doesn't work.

connect(pushButton_redraw_dist_plots, SIGNAL(clicked(bool)), this, SLOT(drawDistPlots(bool)));

If your slot does not use the bool argument, then there is no need to define the slot method with a bool argument. That is, your slot can simply be:

void MyClass::drawDistPlots()

The Qt signal / slot system allows slots to use fewer arguments than the signals they are connected to. For example, if the signal has two parameters, then the slot can use both of them, only the first one, or none at all.