我通过子类化 QLineEdit 和 QCalendar 创建了一个日期输入。当收到 mousePressEvent 时,日历会显示在 QLineEdit 的底部。问题在于隐藏该日历。我已经覆盖了它的 focusOutEvent,因为我希望它在用户单击其他地方时关闭。但是根本没有收到这个事件,我通过在其中设置断点来确认这一点,它永远不会停在那里。我已经打电话给close()
它:
class MyCalendarWidget : public QCalendarWidget
{
Q_OBJECT
public:
void focusOutEvent(QFocusEvent* e)
{
close();
}
};
当我从 DateLineEdit 关闭它时,它按预期工作:
void DateLineEdit::mousePressEvent(QMouseEvent *)
{
if (calendar->isVisible())
{
calendar->close();
}
else
{
calendar->move(mapToGlobal(QPoint(0, height())));
calendar->show();
}
}