天天看點

Qt視窗失去焦點關閉/點選視窗外其他地方關閉

點選視窗外其他地方關閉

setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);
           

視窗失去焦點關閉

1.示例一

bool testWidget::eventFilter(QObject *obj, QEvent *event)
{
	if (Q_NULLPTR == obj) {
		return false;
	}
    if (QEvent::ActivationChange == event->type()) {
        if(QApplication::activeWindow() != this){
            this->close();
        }
    }
	return QWidget::eventFilter(obj, event);
}
           

2.示例二

bool testWidget::event(QEvent * event)
{
  	if (QEvent::ActivationChange == event->type()) {
		if (QApplication::activeWindow() != this){
			this->close();
		}
	}

	return QWidget::event(event);
}