您的位置:首页 > 编程语言 > Qt开发

QT 实现点击窗口以外任何位置即关闭窗口

2017-11-20 11:07 495 查看
bool QTipLabel::eventFilter(QObject *o, QEvent *e)
{
switch (e->type()) {
#ifdef Q_DEAD_CODE_FROM_QT4_MAC
case QEvent::KeyPress:
case QEvent::KeyRelease: {
int key = static_cast<QKeyEvent *>(e)->key();
Qt::KeyboardModifiers mody = static_cast<QKeyEvent *>(e)->modifiers();
if (!(mody & Qt::KeyboardModifierMask)
&& key != Qt::Key_Shift && key != Qt::Key_Control
&& key != Qt::Key_Alt && key != Qt::Key_Meta)
hideTip();
break;
}
#endif
case QEvent::Leave:
hideTip();
break;

#if defined (Q_OS_QNX) // On QNX the window activate and focus events are delayed and will appear
// after the window is shown.
case QEvent::WindowActivate:
case QEvent::FocusIn:
return false;
case QEvent::WindowDeactivate:
if (o != this)
return false;
hideTipImmediately();
break;
case QEvent::FocusOut:
if (reinterpret_cast<QWindow*>(o) != windowHandle())
return false;
hideTipImmediately();
break;
#else
case QEvent::WindowActivate:
case QEvent::WindowDeactivate:
case QEvent::FocusIn:
case QEvent::FocusOut:
#endif
case QEvent::Close: // For QTBUG-55523 (QQC) specifically: Hide tooltip when windows are closed
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
case QEvent::MouseButtonDblClick:
case QEvent::Wheel:
hideTipImmediately();
break;

case QEvent::MouseMove:
if (o == widget && !rect.isNull() && !rect.contains(static_cast<QMouseEvent*>(e)->pos()))
hideTip();
default:
break;
}
return false;
}


然后在初始化时 注册 eventfilter

qApp->installEventFilter(this);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐