PDA

View Full Version : Need a signal almost like editingFinished



eric.frederich
25th February 2011, 15:34
Looking at the signals available from QLineEdit for triggering something to happen when the text changes there are the following.

void editingFinished ()
void textChanged ( const QString & text )
void textEdited ( const QString & text )

I need to connect to a signal that is only emitted when the editing is actually finished, not as each character is entered. For this, editingFinished() works. My problem right now is that editingFinished() also gets called when a user tab's through the line edit gaining and then losing focus. I don't want my slot to be called unless the text actually changed.

What do I need to do to make this happen? Do I need to subclass QLineEdit, do I need to override some event handlers?
I'm pretty lost right now. I hope it'll be simple.

Thanks,
~Eric

high_flyer
25th February 2011, 16:15
One way is to subclass QLineEdit.
Another way, which I think in this case is better suited, is to do the logic on your side.
This means, you connect both to textEdited() and editingFinished() - and in the receiving slots, you flag a bool (m_bTextEdited and m_bEditingFinished respectively).
In your current slot which is now being called on editigFinished() you examine the two booleans, and execute the code in the slot only if both are true.
Don't forget to reset the flags when you are done.

eric.frederich
25th February 2011, 16:45
One way is to subclass QLineEdit.
Another way, which I think in this case is better suited, is to do the logic on your side.
This means, you connect both to textEdited() and editingFinished() - and in the receiving slots, you flag a bool (m_bTextEdited and m_bEditingFinished respectively).
In your current slot which is now being called on editigFinished() you examine the two booleans, and execute the code in the slot only if both are true.
Don't forget to reset the flags when you are done.

Thanks a bunch, that worked like a charm. Only needed one boolean though for the textEdited since my code was already attached to editingFinished the editingFinished boolean would always be true anyway.

Edit:
Out of curiosity, what was your m_b supposed to stand for?

wysota
25th February 2011, 16:50
There is also the QLineEdit::modified property that you can use. You can reimplement focusInEvent and focusOutEvent and clear the flag on focus in and check the flag on focus out and emit a signal. Or you can use modified directly with editingFinished() if it suits your needs.

high_flyer
25th February 2011, 18:46
Out of curiosity, what was your m_b supposed to stand for?
This is the Hungarian notation (http://www.learncpp.com/cpp-tutorial/29-hungarian-notation/).