Results 1 to 14 of 14

Thread: slot return type

  1. #1
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default slot return type

    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

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: slot return type

    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....
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slot return type

    Ok, so if I want to save the value can i do this?

    char* var;

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

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: slot return type

    no, you can not do this. you need implement a slot and then store value, e.g.
    Qt Code:
    1. ...
    2. connect(timer1, SIGNAL(timeOut()),this, SLOT(readSlot()));
    3. ...
    4. void MyObj::readSlot()//slot
    5. {
    6. const char *var = read();
    7. ...
    8. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slot return type

    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()));

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: slot return type

    make this variable as class member.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slot return type

    with class member, do you mean define it with the class atributes?

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: slot return type

    Quote Originally Posted by adamatic View Post
    with class member, do you mean define it with the class atributes?
    variables which declared in class called as class members, e.g.
    Qt Code:
    1. class foo {
    2. ...
    3. private:
    4. int m_value;//class member
    5. };
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: slot return type

    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.

  10. #10
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slot return type

    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:

    Qt Code:
    1. ..............................
    2. connect(this,SIGNAL(comeback()),this,function1());
    3. ....................
    4.  
    5. void function1(){
    6. ....
    7. connect(timer1, SIGNAL(timeOut()),this, SLOT(readSlot()));
    8. char *var=read();
    9. ....
    10. }
    11. readSlot(){
    12. emit comeback();
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 17th February 2009 at 09:55. Reason: reformatted to look better

  11. #11
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: slot return type

    i think wysota is saying this:

    Qt Code:
    1. connect(A, SIGNAL(b()), A, SLOT(d()));
    2. connect(A, SIGNAL(customSignal(urValue)), A, SLOT(anotherSlot(urValue)));
    3.  
    4. d()
    5. {
    6. .
    7. .
    8. .
    9. emit customSignal(&valueYouWantToReturn);
    10. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: slot return type

    Something like that, yeah...

    An example of transmitting a bool value follows...
    Qt Code:
    1. connect(A, SIGNAL(someSignal()), B, SLOT(execute()));
    2. connect(B, SIGNAL(executeFinished(bool)), A, processExecuteResult(bool)));
    3. //...
    4. bool BClass::execute(){
    5. bool result = doSomething();
    6. emit executeFinished(result);
    7. return result;
    8. }
    To copy to clipboard, switch view to plain text mode 

    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).

  13. The following user says thank you to wysota for this useful post:

    adamatic (17th February 2009)

  14. #13
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: slot return type

    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.

  15. #14
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: slot return type

    Quote Originally Posted by wysota View Post
    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

Similar Threads

  1. error with QList with a class
    By john_god in forum Newbie
    Replies: 7
    Last Post: 12th January 2009, 21:48
  2. QTableView performances
    By miraks in forum Qt Programming
    Replies: 18
    Last Post: 1st December 2008, 10:25
  3. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  4. dummy question(Error)
    By Masih in forum Qt Programming
    Replies: 12
    Last Post: 19th July 2007, 23:38
  5. Invalid return type
    By Salazaar in forum Newbie
    Replies: 3
    Last Post: 5th June 2007, 17:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.