PDA

View Full Version : How to get the return value when signal emit.



prabhatjha
10th April 2020, 08:27
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";
}
}

d_stranz
10th April 2020, 17:01
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:



// MyClass.h

class MyClass : public QObject
{
Q_OBJECT

// ...

signals:
void mySignal( int & result );

};

// MyClass.cpp

int MyClass::someFunction()
{
int result = 0; // ALWAYS initialize in case the slot doesn't update "result"

emit mySignal( result );
if ( result != 0 )
{
// do something
}
}


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.

prabhatjha
11th April 2020, 16:13
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.