Slot is being called multiple times every time a signal is emitted
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).
Would the slot get called multiple times if I connect the same slots for multiple times?
Short answer: Your slot would be called twice.
Whether this causes bugs or is the desired behaviour depends on your application of course.
In most cases it is probably not wanted, so to prevent this you can either track your connections, use disconnect first or if you are using at least Qt 4.6 there is a new connection type Qt::UniqueConnection
which prevents duplicate connections automatically and otherwise behaves like Qt::AutoConnection
, see connection types and a blog post about the new unique type.
Qt Signals and Slot connected twice... what happens?
A few weeks ago, we had an intern accidentally connect a signal to a slot more than once. The idea was that under one condition, you'd have the slot connected to the signal, and under another condition you'd disconnect it. When you changed modes, you'd do the appropriate work.
Well, he forgot to to disconnect when appropriate. So every time you changed modes, you had a new connection to the slot.
The end result? 1 connection == 1 call to slot. 2 connections == 2 calls to slot. 3 connections == 3 calls to slot, etc. These calls happened "simultaneously" (I know in actuality they did not since they are on the same event thread, but what I mean was all calls were processed in succession).
As @Job points out in one of his comments (he deserves credit, so please do not upvote me for his work), Qt::UniqueConnection
will prevent this issue.
QT SLOT called 1,2,3... times
You are creating a new Dialog
everytime you call on_actionTooo_triggered()
. This Dialog
isn't deleted at the end of your function. Therefore with the next call the signal MainWindow::s1(QString)
is emitted to the two different Dialog
s which results in multiple ouptuts in qDebug
.
Related Topics
Conditional Operator Used in Cout Statement
Polymorphic_Allocator: When and Why Should I Use It
C++ Error: '_Mm_Sin_Ps' Was Not Declared in This Scope
C++ Class Member Function Pointer to Function Pointer
Calling R's Optim Function from Within C++ Using Rcpp
Choosing Embedded Scripting Language for C++
How to Rearrange a String Equation
C++ Inherit from Multiple Base Classes with the Same Virtual Function Name
Libpng, Palette Png with Alpha or Not
Understanding the Example on Lvalue-To-Rvalue Conversion
Unoptimized Clang++ Code Generates Unneeded "Movl $0, -4(%Rbp)" in a Trivial Main()
Downcasting Shared_Ptr<Base> to Shared_Ptr<Derived>
Lambda Capture as Const Reference
Why Does Clang Optimize Away X * 1.0 But Not X + 0.0