Results 1 to 3 of 3

Thread: How to get the return value when signal emit.

  1. #1
    Join Date
    Aug 2013
    Location
    India
    Posts
    44
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default How to get the return value when signal emit.

    Hello Everyone,

    I have one thread function. I am emitting a signal which is showing messagebox with yes and no button.
    How i can know in thread cleaning function which button was pressed.


    bool MainWindow::thread_cleaning()
    {
    int retvalue = emit sigMsgBoxOpen(); // MessageBOX open slot will call

    if (retvalue == 0) // check Yes button pressed in messageBox
    {

    qDebug()<<"Yes button pressed";
    }
    else
    {
    qDebug()<<"NO button pressed";
    }
    }

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to get the return value when signal emit.

    All signals and slots have a "void" return value. You cannot return anything that way.

    What you should do is define your signal so that it takes a reference argument that a slot can fill:

    Qt Code:
    1. // MyClass.h
    2.  
    3. class MyClass : public QObject
    4. {
    5. Q_OBJECT
    6.  
    7. // ...
    8.  
    9. signals:
    10. void mySignal( int & result );
    11.  
    12. };
    13.  
    14. // MyClass.cpp
    15.  
    16. int MyClass::someFunction()
    17. {
    18. int result = 0; // ALWAYS initialize in case the slot doesn't update "result"
    19.  
    20. emit mySignal( result );
    21. if ( result != 0 )
    22. {
    23. // do something
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    This code assumes that there is only one slot that will be connected to the signal. If more than one slot is connected, and each can set "result" to something different, then the only value you will get is for the slot that was connected last since signals are handled by slots in the order in which they were connected.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Aug 2013
    Location
    India
    Posts
    44
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get the return value when signal emit.

    Thank you so much for your reply.
    I did the same what you have suggested.
    int result = 0; // ALWAYS initialize in case the slot doesn't update "result"

    emit mySignal( result );

    but getting error. warning:QObject::connect: cannot queue arguments of type 'ínt&'(Make sure 'int&' is registered using qRegisterMetatype())
    please assit me how to register int & in qRegisterMetatype and where we need to resgister.

Similar Threads

  1. Emit Signal From Another Class
    By mardi in forum Newbie
    Replies: 5
    Last Post: 24th December 2015, 12:17
  2. Seems that my emit doesn't emit the signal
    By roseicollis in forum Newbie
    Replies: 2
    Last Post: 19th January 2015, 16:05
  3. Replies: 2
    Last Post: 3rd May 2011, 20:22
  4. Emit signal from thread
    By sisco in forum Newbie
    Replies: 2
    Last Post: 26th November 2009, 13:32
  5. emit a signal
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2006, 11:14

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.