-1

Consider functions like below:

class1::class1()
{
    class3 obj3 = new class3(this);
}
void class1::function1()
{
    class2 *obj2 = new class2();

    connect(obj3, SIGNAL(sig()), obj2, SLOT(slt()));

}

void class3::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.....

Nikhil Singh
  • 81
  • 10
  • Possible duplicate of [Slot is being called multiple times every time a signal is emitted](http://stackoverflow.com/questions/10975247/slot-is-being-called-multiple-times-every-time-a-signal-is-emitted) – David Grayson Jul 14 '16 at 06:18

2 Answers2

2

You should only call connect once. Move your code that calls connect to a different function that is only called once.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • Well @David I know that but unfortunately I have to do this way. so is it possible? – Nikhil Singh Jul 14 '16 at 06:06
  • 1
    @NikhilSingh - really?... well, if have absolutely have to keep it in this function then you have to ensure it is only called once - e,g, add a static variable `static bool connectOnFirstCall = true`. Then if connectOnFirstCall is true do the connect() and then set connectOnFirstCall to false. If you need to connect / disconnect multiple times then you will need a member (global) variable to keep track of it.... +1 to David's answer because it is the correct way to do it usually. – code_fodder Jul 14 '16 at 07:13
  • If you really have to do it this way your design is broken and you should think about redesigning your code! – Mailerdaimon Jul 14 '16 at 08:16
1

So I´m not sure you understand what you are doing or what you want. So I´ll explain what is happening and then we can rethink what you want. Qt is working as expected. You said you want the slot to be called whenever the signal is emitted. That is exactly what your code is doing.

Now the question is ... are you aware that you create a new instance of your class2 each time function1 is called? Are you sure you didn´t just want a single instance of class2? Because that would then behave more in the way that you want it to.

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

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

Since I have no idea where you create an object of class1, I just gave you something based on what you gave us. This should behave in the way you want it to. If that confuses you, I would suggest you should google some tutorials on learning C++, or object oriented programming (OOP) for that matter. Unfortunately I cannot recommend you any books/tutorials or else I would.

I hope this helps you.

Sir Rogers
  • 345
  • 1
  • 3
  • 10
  • well I have just edited my question. I'll explain you can consider class1 as mainwindow and class2 as a tab. function1() is called from the menu option in the mainwindow, which will create a new tab(class2). This tab is browsing a text file for some operation. when tis operation is finished I want to emit a signal sig() and close the tab opened which browsed the text file in the slot slt(). I hope it will give you clear pitcureof what I want to do. Any help will be appreciated – Nikhil Singh Jul 14 '16 at 09:16