Avoid signal editingFinished() to be sent twice on hide()
-
I have a
QLineEdit
widget and when it looses focus or editing is finished by pressing the enter key it should disappear. So I connected a slot to it where I call thehide()
method. With a fewqDebug
messages I have noticed that the signaleditingFinished()
is sent twice; the first time it is sent when I finish editing or the widget looses focus, the second time it is sent when I callhide()
on the widget. The double-call does not have any side-effects on my code, but the slot has a few tasks that might require some time to complete, and I would like to avoid doing twice those tasks.How can I avoid sending the
editingFinished()
signal when I callhide()
? -
Hi,
You can call:
lineEdit->blockSignals(true); lineEdit->hide(); lineEdit->blockSignals(false);
Hope it helps
-
You're welcome !
Since it's working as you want it, please update the thread title prepending [solved] so other forum users may know a solution has been found :)
-
Hi,
@SGaist
Regarding your answer.
Do you have any idea how can I do it in elegant way if it's not situation when I closing or hiding window.
I have situation where the slot is called twice if user performs action.
If I start block signaling it works and action is performed only once, but I need to unblock signaling for further user operations.My first idea is to start timer for unlocking signaling.
Do you maybe know other way for doing that?
BR,
Kaluss -
Hi,
What exactly is your use case ?
-
@SGaist
I got function:void calculateSLot() {}
It's connected to editingFinished() signal of QLineEdit object.
When calculations will be performed and function will exit, my system must be ready to processing next actions.If I use above examples the second call of the function is executed anyway.
At the moment my only idea is to set timer and block signal on function exit and unblock it when timer expires. -
Did you check that you have connected editingFinished() only once?
The initial post has been concerning to avoid the emit of signal editingFinished under the condition of hiding. Sometimes there duplicated signals when you are not carefully choosing multiple signals used. Under such conditions it may be good to block signals or when signals are emitted while you set values.
However, when you are using only editingFinished() there should be only one call to your slot. Therefore, when editingFinished() is always the source for calling you should better check why this is the case.
There are the dump routines for QObject dumpObjectTree and dumpObjectInfo helping you to inspect the connections to objects.
Your choice of using QTimer may work, but there different reasons that this might be a problem. For very short calculations that may sufficient, but longer calculations this may be system dependent. Waiting too short will not help and waiting too long may confuse the user.