PDA

View Full Version : Spurious signals sent when menu item selected



Eos Pengwern
4th March 2011, 02:12
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.

- how do I stop it?

Many thanks,

Stephen.

MarekR22
4th March 2011, 09:13
Instead telling a long story, give us some simple solid facts, like WHAT ARE THE NAMES OF THIS SIGNALS you are interested in? What is the reason to listen those signals?

Eos Pengwern
4th March 2011, 11:01
OK, fixed this...

About 80% of the widgets on my GUI are subclasses of QLineEdit, and the problem was in fact only occurring with them. The offending signal was QLineEdit::editingFinished(), which according to the documentation is emitted “when the Return or Enter key is pressed or the line edit loses focus” (my emphasis).

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 was that the signal was being emitted when I pressed the Enter key (which I always do as a matter of habit) and again when the widget lost focus.

So I added some code to my subclass to intercept QLineEdit::editingFinished() and check whether the contents had changed since the signal was last emitted, before forwarding the signal if and only if the contents had changed.

wysota
4th March 2011, 11:45
You can use QLineEdit::modified property for tracking changes.

MarekR22
4th March 2011, 13:30
Review documentation of QLineEdit, maybe you need different signal.
For example check QLineEdit::textEdited.
We don't know what are you trying to achieve, you told us how you try to achieve something and you have problem with it. This kind of approach always give a poor results.