PDA

View Full Version : signal/slot different approach



salmanmanekia
13th August 2008, 10:40
what's the difference between the two declaration below

connect(object1,SIGNAL(clicked()),SIGNAL(mySignal( )));

connect(object1,SIGNAL(clicked()),this,SLOT(mySlot ()));
void mySlot()
{
emit mySignal();
}
which is preferred and why..

spirit
13th August 2008, 10:56
what's the difference between the two declaration below

connect(object1,SIGNAL(clicked()),SIGNAL(mySignal( )));

connect(object1,SIGNAL(clicked()),this,SLOT(mySlot ()));
void mySlot()
{
emit mySignal();
}
which is preferred and why..

the third parameter (this) is the receiver, if reciver isn't specify than an object, in which connect is used, is used by default.

wysota
13th August 2008, 11:58
which is preferred and why..

First one is cleaner and will be one invokation faster than the other.

salmanmanekia
13th August 2008, 12:15
First one is cleaner and will be one invokation faster than the other.
can you please elobrate on 'invokation'

lyuts
13th August 2008, 13:10
what's the difference between the two declaration below

connect(object1,SIGNAL(clicked()),SIGNAL(mySignal( )));

connect(object1,SIGNAL(clicked()),this,SLOT(mySlot ()));
void mySlot()
{
emit mySignal();
}
which is preferred and why..

The difference is that the first connect connects a signal to another signal (with "this" as a receiver). It means that mySignal will be emited as soon as clicked signal emits. I think usually further action would be to connect mySignal to a slot (just like the 2nd connect statement).

wysota
13th August 2008, 13:19
can you please elobrate on 'invokation'

Function call. Signal emition is a function call and so is slot activation, so if you don't activate the slot and instead emit the signal directly, you'll have one function call less (which is probably saving about up to 20 cpu cycles, so it's completely irrelevant).