0

I have a QDateEdit with the calendar enabled and am trying to capture the end of editing:

the_date = QDateEdit(...)
<some more initialization>
the_date.setCalendarPopup(True)
the_date.editingFinished.connect(checkDate)
...
def checkDate():
  print ("checkDate called")

If I edit the date from the keyboard, checkDate() is called when focus leaves the widget by tabbing, hitting return, etc. But if I click on the down arrow that forces display of the calendar, checkDate() is called immediately when the calendar appears, and again when the widget loses focus. I don't want to tie to the userDateChanged because that signals on every keystroke in the edit box.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Llaves
  • 683
  • 7
  • 20

2 Answers2

0

You can save the calendar widget from the QDateTime and check if that's where focus shifted:

the_date = QDateEdit(...)
<some more initialization>
the_date.setCalendarPopup(True)
calendar = the_date.calendarWidget()
the_date.editingFinished.connect(checkDate)
...
def checkDate():
  if not calendar.hasFocus()
    # do whatever it was you wanted to do when QDateEdit finished editing
Llaves
  • 683
  • 7
  • 20
0

QDateEdit inherits from QDateTimeEdit, which in turn inherits from QAbstractSpinBox, which has the keyboardTracking property (enabled by default):

If keyboard tracking is disabled, the spinbox doesn't emit the valueChanged() and textChanged() signals while typing. It emits the signals later, when the return key is pressed, when keyboard focus is lost, or when other spinbox functionality is used, e.g. pressing an arrow key.

The following will provide what you need, without checking the popup focus:

    the_date.setKeyboardTracking(False)

Consider that while your solution might be correct, it's always better to check for the popup dynamically:

    if not the_date.calendarWidget().hasFocus():
        # ...
musicamante
  • 41,230
  • 6
  • 33
  • 58
  • thanks, that's the key piece I was missing. I guess I need to look farther up the inheritance chain – Llaves Nov 24 '21 at 15:55
  • replied before fully testing. ``QAbtractSpinBox`` does not have either ``valueChanged()`` or ``textChanged()`` signals, so they are not inherited by ``QDateEdit``. Those signals belong to ``QSpinBox`` – Llaves Nov 24 '21 at 22:40
  • @Llaves that reference is just to explain the behavior of the spinbox, it works in the same way for the changed signals of QDateTimeEdit. The difference is that it has a further control (the popup) which might trigger the changed signals due to the focus change, *if* the date has changed in the meantime. – musicamante Nov 24 '21 at 23:02
  • Bitten again by the inheritance chain, and thinking to literally. I was looking for ``valueChanged`` or ``textChanged``, I missed ``dateChanged`` in ``QDateTimeEdit``. Thanks for your patience. – Llaves Nov 25 '21 at 00:43