Solved Argument in connect
-
All the answers you are looking for are shown in the Signals & Slots chapter of Qt's documentation.
Don't mix the connection "action" with the signals and slots parameters. While related they are distinct.
Signals and slots can have parameters. And you can connect compatible signals and slots.
-
@mrjj
so after emit signal with parametres,
the slot is call by a classique connect but the definiton of slot is like this :void mySignal2(QString s, int v1, int v2); ... emit mySignal2("the string", 100, 50) ... connect(mySender, &Sender::mySignal2, receiver, &Receiver::slot); void slot(QString s, int v1, int v2){ }
is that ?
ty -
@SGaist
my question may be wrong, I had my answers thank you -
@martial123
Yes, that's it.
The signal and slot must match in parameters.
So you could not say hook up buttons clicked signal to
void slot(QString s, int v1, int v2)
as the button would have no way of adding the string and ints.
However for your own signals and slots. it is of cause allowed. -
@mrjj
ok added
tyif i need to send parametres with signal by clicked for exemple,
i do this:in constructor : connect::(QPushButton, &QPushButton::clicked, this, &class::slot) void slot () { emit mySignal2("the string", 100, 50) } // in main i receive this signal perso and i can connect to a other slot with parametre
-
@martial123
Yes.
You can also use lambdas to avoid creating a slot for that.connect(QPushButton, &QPushButton::clicked, this, [](){ emit mySignal2("the string", 100, 50); });
-
@mrjj
ooh its very useful ! ty i write down this ;) -
@martial123
A note on lambs.
If you want to access anything from the class where
you connect in, you have to capture the data.
likeconnect(ui->pushButton, &QPushButton::clicked, this, [this](){ this->ui->widget->xxxx });
note the [this]
here we capture "this" . any variable can be captured. -
@mrjj
Hum, i don't understand all , but i write down if in the future i need it .. -
@martial123
well before using them, read a bit about it.
It is not super complicated and can save you some slots.
The capture part sounds odd, but basically we just list
[var1, var2,var3 ]
to have them avialable inside the lambda.
It is a free-floating function so it's not part of the class like a slot is.