Solved How to set value to a variable in QtObject::connect
-
Hi i want set value to a variable in a connection
this my code but don't workfloat size; connect(GFS, SIGNAL (completed(float)), size = float);
Is this possible?
-
-
But where is the "size" ?
Is it a member of a class that is QObject? If yes- the fastest way is to use lambda
like this// code somewhere inside a scope of the QObject instance connect(sender, &Sender::someFloatDataChanged, [=](float newData) { this->a = newData; });
-
i do this before like this :
connect(GFS, SIGNAL (completed(float)), this, SLOT (setSize(float)));
completed signal have value & slot setSize get this value but i want set this value to a variable
-
@BePie more or less showed you how exactly to do it.
But here again:
First your variable to store/hold the data must either be declared in your header or in the function where you make the QObject::connect();
float mySize;
than adjust your connect statement, first part - Pointer to Object and Signal - stays the same as always.
connect(GFS, &myGfsClass::completed, ...
than comes the lambda fuction in {}-brackets + correct syntax for your variable, the float from the completed SIGNAL.
... [=](float value){mySize =value;});
and done.
-
so w/o explicit slot creation you can write:
connect(GFS, &GFS_T::completed, [=](float newData){setSize(newData);}));
where GFS_T is a type of GFS
or paste code from setSize() to the body of the lambda -
@BePie thank you but GFS is type of a class
FileSize *GFS;
-
so you write
connect(GFS, &FileSize::completed, [=](float newData){setSize(newData);}));
so &FileSize::completed is a reference to signal-function of type (class) FileSize
the last argument is a lambda with copy ([=]) scope that takes one float (must match FileSize::completed arguments) and invokes setSize (implicitely of "this" pointer that is copied ([=])) with passed argument from signal.
I am not sure that I am 100% correct, I am not a pro lambda user. For more info check out some lambda tutorials.but FileSize has to inherit from QObject (otherwise signal-slots won't magically work :P)
// edited: QOpenGL->QObject and added some notes