slot is called multiple times when signal is emitted



  • Consider functions like below:

    void class1::function1()
    {
    class2 *obj = new class2();
    connect(this, SIGNAL(sig()), obj, SLOT(slt()));
    }

    void class2::function2()
    {
    emit sig();
    }
    I am invoking function1() multiple times. function2() is also triggered multiple times

    What I want is, whenever sig() is emitted slt() should be called, but what's happening is, first time when sig() is emitted slt() is being called number of times function1() is invoked. Next time when sig() is invoked slt() is not called. It would be a great help, if somebody can help me achieve this.....



  • @NIXIN said:

    connect(this, SIGNAL(sig()), obj, SLOT(slt()));

    Do the following.
    connect(this, SIGNAL(sig()), obj, SLOT(slt()),Qt::UniqueConnection);

    It will fix your issue.


  • Moderators

    @NIXIN
    in your function1() you creating objects on the heap and connect their signals to some of your slots. So the slot is called for each connection made.
    But from your code it's not clear if you delete the objects when you don't need them anymore. If not the connection is still alive until you call disconnect or one of the objects is destroyed.

    @NIXIN said:

    Next time when sig() is invoked slt() is not called. It would be a great help, if somebody can help me achieve this.....

    Please clarify "next time". What happens in the meantime?



  • @dheerendra well I have tried that, but it does not work.
    I think Qt::UniqueConnection works when same object is used multiple times but here every time when function1() is invoked a new object of class2 is created



  • I did not observe. That is the issue. Why are you creating the new object overtime. Since you are creating the object on the heap, old object is not deleted. So old object still responds for signal. Create the object in constructor and use it everywhere.



  • @raven-worx yes you are right, in the slot I am deleting the object. why I am doing this is because class2 is actually a tab and when sig() is emitted I want to close that tab.
    In function1() I am creating this tab which is taking some files. That's why whenever I have to read a file I have to call function1().
    Please help me to solve the problem....


  • Moderators

    @NIXIN
    this doesn't seem to be correct no?

    class2 *obj = new class2();
    connect(this, SIGNAL(sig()), obj, SLOT(slt()));
    

    The sender should be "obj" instead of "this"?!

    But you should already see a warning message (that the connect() failed becasue the signal isn't available on this class) in the console if this is the case...



  • @raven-worx Yes, again u r right
    You can consider something like this:

    class1::class1()
    {
    class3 ob = new class3(this);
    }

    void class1::function1()
    {
    class2 *obj = new class2();
    connect(ob, SIGNAL(sig()), obj, SLOT(slt()));
    }

    void class3::function2()
    {
    emit sig();
    }


  • Moderators

    @NIXIN
    well...if you still expect some help how about to post your actual code?
    Instead of some pseudo code (which only contains lines which YOU think are relevant) which probably isn't even correct... i am tired of guessing.

    Again...is a connection on itself really what you want?!

    class2 *obj = new class2();
    connect(obj, SIGNAL(sig()), obj, SLOT(slt()));
    


  • @raven-worx No, I want a connection between objects of class2 and class3



  • if u want to establish a connectin between the objects2 and objects3,

    as u said earlier u want to close te tab of class 2

    class2::class2{

    // create the object of class 3 here in constructor.

    Class3 obj3* = new Class3;

    connect(obj3,SIGNAL(signal()),this,SLOT(close()),Qt::UniqueConnection);

    }

    class3::class3
    {
    }

    class3::function()
    {

    emit signal();
    }

    so the object will be created in constructor of Class2.
    as the signal is emmitted , and when it is triggered the class2 will be closed. as per the post which u want to close the tab when signal is emmitted.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.