Skip to content

Instantly share code, notes, and snippets.

@huihut
Created August 21, 2019 03:15
Show Gist options
  • Save huihut/7e91776049231c7164f8f725fc9ebb1a to your computer and use it in GitHub Desktop.
Save huihut/7e91776049231c7164f8f725fc9ebb1a to your computer and use it in GitHub Desktop.
Qt Dialog 点击窗口外隐藏
// 重写事件
bool event(QEvent *event)
{
if (event->type() == QEvent::ActivationChange)
{
if (QApplication::activeWindow() != this)
{
this->hide();
}
}
return QWidget::event(event);
}
// 重写事件过滤器
// 需要初始化过滤器:installEventFilter(this);
bool eventFilter(QObject *o, QEvent *e)
{
if (QEvent::WindowDeactivate == e->type() && QApplication::activeWindow() != this)
{
hide();
}
return QWidget::eventFilter(o, e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment