PDA

View Full Version : Is it possible to define / implement signal in Qt ?



keshav2010
24th January 2018, 22:53
What i really want to know is, is it possible to actually "define" signal and have an implementation for it in *.cpp file rather then just having a declaration in .h file, to me it seems like as if it is a completely impossible to have implementation for signal functions
like for example

connect( someObject, SIGNAL( mySignal() ), otherObject , SLOT (otherSlot()));

now, if implementation of mySignal is as follow :

mySignal()
{
qDebug()<<" hello ";
}

i want to ask, if i emit mySignal(), will it's own body will be executed before the slot function is executed or im getting it completely wrong and signal serve no purpose other than just as an signature that can carry around values and have no implementation of its own.

Santosh Reddy
25th January 2018, 07:56
Nice thought :)


...is it possible to actually "define" signal..
Yes, you can define, but then you will have 2 implantations of the signal, one provided by Qt (qmake) and one by you, so eventually linker will throw error and your program will not build.


..if i emit mySignal(), will it's own body will be executed before the slot function is executed...
When you emit a signal (which is nothing but calling the signal function), signal's body is executed, and in the signal's body there is code to call all the connected slots.


// following statements are same
emit mySignal;
mySignal();




...im getting it completely wrong and signal serve no purpose other than just as an signature that can carry around values and have no implementation of its own.

keywork "signal" is used inform the qmake that the following member functions are signals and that the functions must be implemented by the qmake.

Basically signal or slot is just a plain cpp member functions. In the context of Qt, qmake provides the implementation of the signals, you don't (and should not) implement the signals. In the Qt provided implementation of the signal, all the connected slots are called.

d_stranz
25th January 2018, 17:55
What i really want to know is, is it possible to actually "define" signal and have an implementation for it in *.cpp file rather then just having a declaration in .h file, to me it seems like as if it is a completely impossible to have implementation for signal functions

To add to what Santosh said, a method declared as a "signal" in a class definition is implemented when your code is pre-compiled by MOC (the Qt Meta-Object Compiler). You can see this code in the "moc_xyz.cpp" (where "xyz" is the name of your class) file that is created in the GeneratedFiles subdirectory of your project. The implementation of your signal created by MOC goes through each of the slots that were connected to your signal, in the order in which they were connected, and calls the slot with the arguments you provided when you emitted your signal.

It is completely boilerplate code, which is why it can be generated simply from the declaration of your signal in the class header.

By asking if you can implement your own code for a signal, I think you misunderstand what signals are for. They are solely to notify some listener (slot) that something has changed.

So if your code needs to do something prior to emitting its signal, then you do it in some method that at the end emits the signal.

high_flyer
26th January 2018, 11:58
+1 to what d_stranz said - very good answer.
Continuing with d_stranz's line, you actually can do something to the effect of what you are talking about by having a regular signal connected to a lambda function slot.
Some more information: https://wiki.qt.io/New_Signal_Slot_Syntax