QCoreApplication* a = QApplication::instance();
if (a)
a->installEventFilter(this);
在application中注册事件过滤
然后重载
bool eventFilter(QObject* watched, QEvent *event)
在eventFilter函数中:
bool YourWindowOrDialog::eventFilter(QObject* watched, QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress) {
QRect rect = rect();
QMouseEvent* e = dynamic_cast<QMouseEvent*>(event);
QPoint pos = mapFromGlobale->globalPos());
if (e && !rect.contains(pos)) {
close();
}
}
return QWidget::eventFilter(watched, event);
}

本文介绍如何在Qt应用程序中使用事件过滤器实现特定的功能,如监听鼠标点击事件并判断点击位置是否在窗口范围内,若不在则关闭窗口。文章通过具体代码示例展示了事件过滤器的安装与重载过程。
2324

被折叠的 条评论
为什么被折叠?



