PDA

View Full Version : slot return type



adamatic
17th February 2009, 09:05
Hello!

I have a basic question, can a slot return a type or the return type has to be always void?

If it can, how could I save the value in a variable?, I mean, if the slot it is call like this:

connect(timer1, SIGNAL(timeOut()),this, SLOT(read()));

And read() return a char*.

Cheers

spirit
17th February 2009, 09:08
slots can return a value. from Qt Assistant


Slots
A slot is called when a signal connected to it is emitted. Slots are normal C++ functions and can be called normally; their only special feature is that signals can be connected to them....

adamatic
17th February 2009, 09:12
Ok, so if I want to save the value can i do this?

char* var;

connect(timer1, SIGNAL(timeOut()),this, SLOT(var=read()));

spirit
17th February 2009, 09:17
no, you can not do this. you need implement a slot and then store value, e.g.


...
connect(timer1, SIGNAL(timeOut()),this, SLOT(readSlot()));
...
void MyObj::readSlot()//slot
{
const char *var = read();
...
}

adamatic
17th February 2009, 09:23
If I want to use the varible in another function, do I have to have var as global varible? . I mean in the same function than implement the
connect(timer1, SIGNAL(timeOut()),this, SLOT(readSlot()));

spirit
17th February 2009, 09:24
make this variable as class member.

adamatic
17th February 2009, 09:35
with class member, do you mean define it with the class atributes?

spirit
17th February 2009, 09:38
with class member, do you mean define it with the class atributes?

variables which declared in class called as class members, e.g.


class foo {
...
private:
int m_value;//class member
};

wysota
17th February 2009, 09:42
Slots can return values but only when they are called as regular methods or using QMetaObject::invokeMethod().

If you need a return value when using the slot function as a slot, then emit a signal from it and catch it back in the original sender.

adamatic
17th February 2009, 10:10
Are you saying come back to the function sender? But this is going to execute all the code in the original sender, no?.

I understand that you say something more o less like this:


..............................
connect(this,SIGNAL(comeback()),this,function1());
....................

void function1(){
....
connect(timer1, SIGNAL(timeOut()),this, SLOT(readSlot()));
char *var=read();
....
}
readSlot(){
emit comeback();
}

talk2amulya
17th February 2009, 10:17
i think wysota is saying this:


connect(A, SIGNAL(b()), A, SLOT(d()));
connect(A, SIGNAL(customSignal(urValue)), A, SLOT(anotherSlot(urValue)));

d()
{
.
.
.
emit customSignal(&valueYouWantToReturn);
}

wysota
17th February 2009, 11:00
Something like that, yeah...

An example of transmitting a bool value follows...

connect(A, SIGNAL(someSignal()), B, SLOT(execute()));
connect(B, SIGNAL(executeFinished(bool)), A, processExecuteResult(bool)));
//...
bool BClass::execute(){
bool result = doSomething();
emit executeFinished(result);
return result;
}

Another approach is to use a solution similar to QFuture just for signals and slots or to emit a signal "resultReady" from the slot so that you don't transmit the result itself but only an information that it is available and you can read it from the object owning the original slot. That's more or less what QFuture does only that the result is available not in the object itself (B in our case) but in a separate object (the future).

adamatic
17th February 2009, 11:12
Ok, I have got the idea. But i have finally defined the variable as class member to have easier access to it.

But thank you anyway because for sure I will use this knowledge in the future.

astodolski
4th October 2014, 22:01
Slots can return values but only when they are called as regular methods or using QMetaObject::invokeMethod().

If you need a return value when using the slot function as a slot, then emit a signal from it and catch it back in the original sender.

I thought that may be the preferred way - seems messy, but I don't know of another way. In my case the original sender lives in a worker object moved to another thread