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 the hide() method. With a few qDebug messages I have noticed that the signal editingFinished() 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 call hide() 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 call hide()?


  • Lifetime Qt Champion

    Hi,

    You can call:

    lineEdit->blockSignals(true);
    lineEdit->hide();
    lineEdit->blockSignals(false);
    

    Hope it helps



  • yes, that worked perfectly, thanks!


  • Lifetime Qt Champion

    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


  • Moderators

    @Kaluss

    How about QSignalBlocker?
    It seems to do what you need.



  • Hi,
    @koahnig
    I will try this, but I suppose it won't work for me, because I tried already to:

    void myFunc(){
    lineEdit->blockSignals(true);
    
    ....
    
    lineEdit->blockSignals(false);
    }
    

    And it doesn't work.


  • Lifetime Qt Champion

    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.


  • Moderators

    @Kaluss

    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.


Log in to reply
 

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