未收到 QWidget focusOutEvent

发布于 2024-10-29 05:19:12 字数 698 浏览 4 评论 0原文

我通过子类化 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩一世无双 2024-11-05 05:19:12

我猜它没有发送 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).

雨夜星沙 2024-11-05 05:19:12

你知道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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文