22

I am using one signal and slot connection in a block. My code as follows

in a.cpp

{
 QObject::connect(m_ptheFlange2Details,SIGNAL(GetFlang1DimAfterAnalysis()),
                 this,SLOT(GetFlang1DimAftrAnalysis()));

 m_ptheFlange2Details->get();// one function inside which i am emiting
                             // GetFlang1DimAfterAnalysis() signal ;

 QObject::disconnect(m_ptheFlange2Details,SIGNAL(GetFlang1DimAfterAnalysis()),
                     this,SLOT(GetFlang1DimAftrAnalysis()));

}

Inside the get() function when this emit statement executes, the slot is called lots of times. Where as according to me it should call only once.

Rick Smith
  • 9,031
  • 15
  • 81
  • 85
Viku
  • 2,845
  • 4
  • 35
  • 63
  • 4
    Have you make sure that `connect` is not called multiple times??!!! Please show us a little bit more code. In which function `connect` and `disconnect` has been written?? – Ammar Jun 11 '12 at 06:24
  • 2
    How many times is `emit` called in `get()`? – cmannett85 Jun 11 '12 at 06:31
  • 3
    This code looks good.Now post the ugly part of your code! – ScarCode Jun 11 '12 at 09:35
  • Check if `QObject::disconnect(...)` returns `true`. If it does, then this part of the code is OK. You may have trouble in `get()`, or elsewhere. – Kuba hasn't forgotten Monica Jun 11 '12 at 16:10
  • i am using only one emit statement inside get() , then how can this slot invoke for lots of time ??QObject::disconnect(--) also returns true . i am not able to understand whats the problem ... shall i go for QObject::uniqueconnection() – Viku Jun 12 '12 at 11:39

1 Answers1

40

As stated in some of the comments, this is usually caused by calling the connect more the once. The slot will be called once for every time the connection is made. For example, the following code will result in slot() being called 3 times when signal() is emitted once.

connect(obj, SIGNAL(signal()), obj2, SLOT(slot()));
connect(obj, SIGNAL(signal()), obj2, SLOT(slot()));
connect(obj, SIGNAL(signal()), obj2, SLOT(slot()));

If you are doing connects in code that may be run more than once, it is generally a good idea to use Qt::UniqueConnection as the 5th parameter. The following code will result in slot() being called 1 time when signal() is emitted once.

connect(obj, SIGNAL(signal()), obj2, SLOT(slot()), Qt::UniqueConnection);
connect(obj, SIGNAL(signal()), obj2, SLOT(slot()), Qt::UniqueConnection);
connect(obj, SIGNAL(signal()), obj2, SLOT(slot()), Qt::UniqueConnection);

I'm guessing the reason your code is not working correctly is because you are omitting the 5th parameter and connect defaults to Qt::DirectConnection (for single threaded programs). This immediately calls the slot as if it were a function call. This means that it is possible for connect to be called again before the disconnect happens (if there are loops in your program).

Rick Smith
  • 9,031
  • 15
  • 81
  • 85
  • if you are emitting a signal , then only the slot is being called right ?? it does not matter how many times you have used the connect statement .. could you please describe something on Qt::uniqueconnection ..since i am new to qt , so dont have more idea on it .. – Viku Jun 14 '12 at 07:29
  • 3
    It *does* matter how many times you call connect *unless* you use Qt::UniqueConnection. If you are doing connects in a function, and you call that function more than once, you need to use Qt::UniqueConnection or your slot will get called multiple times. I edited my post to try to show that a little better. – Rick Smith Jun 14 '12 at 21:37