PDA

View Full Version : Nested signals and slots



vishva
18th August 2006, 07:29
hai friends,

I am suffering the following problem in my application.

Code:

Header file:

class Sample: public QDialog
{

private slots:

void fun1();

void fun2();

};

cpp file:

Sample::Sample
{

connect(&object,SIGNAL(clicked()),this,SLOT(fun1()));
}

void Sample::fun1()
{
connect(&object,SIGNAL(clicked()),this,SLOT(fun2()));
}

void Sample::fun2()
{

//
// to run some instruction here
}


Ok, run this program,

First time,
Main widget( Clicked) -> fun1() -> clicked -> fun2() then return to main widget
Here ,its work fine.

Second time,

Main widget( Clicked) -> fun1() -> clicked -> fun2() then again call fun2() , then return to main widget
3rd time,
the function2 called 3 times.

Nth time ,

The function2 called n times.


i.e,
the last function ( fun2() ) called number of times automatically ( current + prev ).


Can you tell me ,what is the problem in nested signals and slots ?

jpn
18th August 2006, 07:58
Can you tell me ,what is the problem in nested signals and slots ?
The problem is that you are forming a new connection every time the fun1() slot is called. The old connections naturally remain unless you disconnect them (http://doc.trolltech.com/4.1/qobject.html#disconnect).

I'm not sure what are you trying to achieve, but maybe you need something like this:


void Sample::fun1()
{
disconnect(&object,SIGNAL(clicked()),this,SLOT(fun1() ));
connect(&object,SIGNAL(clicked()),this,SLOT(fun2() ));
}

vishva
18th August 2006, 09:47
hi jpn,

i sloved that problem using disconnect() in fun2(). so it works fine.

Thanks