未收到 QWidget focusOutEvent
我通过子类化 QLineEdit 和 QCalendar 创建了日期输入。当在 QLineEdit 上接收到 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();
}
}
I've created a date input by subclassing a QLineEdit and a QCalendar. The calender is displayed at the bottom of the QLineEdit when a mousePressEvent is received on it. The problem is with hiding that calendar. I've overridden its focusOutEvent as I want it to be closed when the user clicks somewhere else. But this event is not received at all, I confirmed this by putting a breakpoint in it, it never stops there. I've put a call to close()
in it:
class MyCalendarWidget : public QCalendarWidget
{
Q_OBJECT
public:
void focusOutEvent(QFocusEvent* e)
{
close();
}
};
When I close it from my DateLineEdit, it works as expected:
void DateLineEdit::mousePressEvent(QMouseEvent *)
{
if (calendar->isVisible())
{
calendar->close();
}
else
{
calendar->move(mapToGlobal(QPoint(0, height())));
calendar->show();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜它没有发送 focusOutEvent 因为它一开始就没有焦点;如果用户随后在 DateLineEdit 中键入某些内容,则肯定不会。从 DateLineEdit 对象捕获 focusOutEvent,并在该点关闭日历;尽管您可能想要测试用户是否单击了日历(在这种情况下它将具有焦点或至少已收到 mousePressEvent)并在这种情况下将其保留(但否则将其关闭)。
I'm guessing it's not sending a focusOutEvent because it never had focus in the first place; certainly not if the user subsequently typed something in the DateLineEdit. Capture the focusOutEvent from the DateLineEdit object, and close the calendar at that point; though perhaps you would want to test whether the user clicked on the calendar (in which case it would have focus or at least have received a mousePressEvent) and leave it up in that case (but otherwise close it).
你知道Qt中已经有这样的东西了吗?请参阅 setCalendarPopup() 和QDateTimeEdit 类的 setCalendarWidget()。我现在能想到的唯一区别是,要查看日历,用户必须单击小部件的右侧,而不是编辑行。
希望这有帮助
Are you aware that there is already such thing in Qt? See the setCalendarPopup() & setCalendarWidget() of the QDateTimeEdit class. The only difference I can thing of now is that to see the calendar the user will have to click on the right side of the widget, instead of the editing line.
Hope this helps