Important: Please read the Qt Code of Conduct -

QWidget signals emitted when menu selected - how do I stop this?

  • In more than one of my Qt applications I've noticed that, whenever the menu bar is clicked, the last signal to have been sent from a widget within the GUI is re-sent before the menu action is invoked. Most of the time this doesn't matter; but on some occasions it matters very much.

    In a few cases where the widget's signal is connected to one of it's own slots, it's straightforward to begin the slot with an

    if (hasFocus()) {


    ...block so that such spurious signals, not generated by the user actually clicking on the widget, can be ignored.

    However, I've recently identified that this behaviour is responsible for several related bugs where the spurious signals are passed on through several layers of the program before being acted upon, so simply checking whether a particular widget has focus is not trivial to implement.

    My question, therefore, is: why on earth does clicking on a menu item cause a signal to be emitted from a widget elsewhere on the screen? I can't find this behaviour documented anywhere? And how do I stop it?

    Many thanks,


    [EDIT: code formatting, Volker]

  • Can you give us a samll, example, showing your problem?

    Which signals are send out from which widget, when you click on a menu item?

    What do you mean by re-send exactly? is it really send twice?

    It is normal, when you click the mouse somewhere, that the focus owner of before, looses focus for this.



  • Thank you for you reply.

    I've noticed in fact that this only seems to be happening with one type of widget in particular - a subclass of QLineEdit which is used for ~80% of the widgets on my GUI. With this the signals are sent multiple times; twice when the value is edited, and then again when any other widget in the GUI (not just a menu item) is selected.

    I'll have a look at the code for that subclass to see if I can figure out what's wrong with it.


  • From which signals exactly are you talking? I'm sure, not all signals are send twice? If you tell us, which are the problematic ones, we could perhaps help or explain...

  • I think I've pinned it down to the the QLineEdit::editingFinished() signal, which according to the documentation is emitted "when the Return or Enter key is pressed or the line edit loses focus".

    This is of course desirable behaviour, as we shouldn't rely on the user pressing the Enter key each time they change a value. My problem is that the signal is being emitted when I press the Enter key (which I do as a matter of habit) and again when the widget loses focus.

    I think I need to add some code to my subclass which intercepts QLineEdit::editingFinished() and checks whether the contents have changed since the signal was last emitted, before forwarding the signal if and only if the contents have changed.


  • Yes indeed, that fixed it.

    Thank you for your input.


Log in to reply