tags:

views:

30

answers:

0

Hi,

I've got an issue with Qt4 focus handling. Since QDateEdit does not support NULL values, I've tried to emulate such a widget by using a QLineEdit with an inputMask. But event with the inputMask the user is still able to enter an invalid date (e.g. 44/44/4444). In that case I want to display an error messagebox when the user leaves the field. If the user clicks 'OK' on the messagebox, the focus should be on the QLineEdit again.

I've tried to register an eventFilter on the QLineEdit and catch the focusOut events. There I parse the date entered by the user and show the error messagebox if needed. If the user presses 'OK' I try to grab the focus again by calling dateField->setFocus(). This works if the user tried to focus a button or another QLineEdit. But if he clicks on a QComboBox, I experience a strange behaviour. The combobox-popup opens after I press 'OK' on the messagebox and I get another focusOut event on the dateField which triggers the messagebox again (infinite loop).

I checked the setFocus() method in QWidget. It is stated that calling setFocus() during a focusOutEvent could lead to infinite recursion. So instead of calling setFocus() directly in the eventFilter method I've tried to delay the call with QTimer::singleShot(0,dateField,SLOT(setFocus())) but without success.

Can somebody tell me how this should be done properly? I've also looked at QValidator but they don't seem to support the exact behaviour I want and AFAIK they are called with every keystroke.

best regards, Michael