0

我有一个启用日历的 QDateEdit,并试图捕捉编辑的结束:

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

如果我从键盘编辑日期,checkDate()当焦点通过 Tab 键离开小部件时被调用,按回车键等。但是如果我点击强制显示日历的向下箭头,checkDate()当日历出现时立即调用,然后再次调用小部件失去焦点。我不想与 userDateChanged 绑定,因为它会在编辑框中的每次击键时发出信号。

4

2 回答 2

0

QDateEdit 继承自 QDateTimeEdit,后者又继承自QAbstractSpinBox,后者具有以下keyboardTracking属性(默认启用):

如果禁用键盘跟踪,则微调框在键入时不会发出 valueChanged() 和 textChanged() 信号。它稍后会在按下返回键、键盘焦点丢失或使用其他旋转框功能(例如按下箭头键)时发出信号。

以下将提供您需要的内容,而无需检查弹出焦点:

    the_date.setKeyboardTracking(False)

考虑到虽然您的解决方案可能是正确的,但最好动态检查弹出窗口:

    if not the_date.calendarWidget().hasFocus():
        # ...
于 2021-11-24T00:14:48.110 回答
0

您可以从 QDateTime 保存日历小部件并检查焦点是否转移:

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
于 2021-11-23T23:38:16.613 回答