gpt4 book ai didi

c++ - 如何在离开(关闭或失去焦点)窗口之前显示模态确认消息?

转载 作者:行者123 更新时间:2023-11-30 05:38:41 25 4
gpt4 key购买 nike

我想在用户离开(alt + tab(关闭或松开焦点))MainWindow 之前显示确认消息框并阻止屏幕。如何做到这一点?

这是我的代码

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QMainWindow::showFullScreen();
this->installEventFilter(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event){
if(event->type() == 128){
QMessageBox::information(this, "title", "text", QMessageBox::Ok | QMessageBox::Cancel);

return true;
}

return true;
}

最佳答案

对于关闭事件:

在您的 MainWindow 类中重新实现 closeEvent 方法。 Link

对于窗口激活和停用事件,请尝试 following :

bool MainWindow::event(QEvent * e) // overloading event(QEvent*) method of QMainWindow 
{
switch(e->type())
{
// ...

case QEvent::WindowActivate :
// gained focus
break ;

case QEvent::WindowDeactivate :
// lost focus
break ;
// ...
} ;
return QMainWindow::event(e) ;
}

关于c++ - 如何在离开(关闭或失去焦点)窗口之前显示模态确认消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32601641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com