Solved Argument in connect
-
If you are talking about "arguments" like QStrings, it is possible. You can pass a string to a slot function
// New sytax: connect(mySender, &Sender::signal, receiver, &Receiver::function); // Signal may look like this in header: signals: void mySignal(QString s);
// Or in "old" style connect(sender, SIGNAL(signal(QString)), receiver, SLOT(function(QString))); // Signal same as above
-
hi ,
In the new syntax, where i send for the slot the QString ??
I need to do that ?:// New sytax: connect(mySender, &Sender::signal, receiver, &Receiver::function( QString s); // Signal may look like this in header: signals: void mySignal(QString s);
-
@SGaist
I just want to know if is it possible because if it's not, it's limited as system.If in future i need to send an object, or variable int or QString, to a slot, how i can do that ?
-
@martial123
Hi
Yes its possible.
as @Pl45m4 shows.
When you use the signal, you give it the parameters values.
emit mySignal("this is the string");
so if we had
void mySignal2(QString s, int v1, int v2);
you would do
emit mySignal("this is the string",100,200);You can have any number of parameters in your signals, including custom types.
-
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.