2015-09-16 27 views
1

我想在用户离开之前显示确认消息框并阻止屏幕(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; 
} 
+1

如果用户“离开”你的窗口,你的窗口失去焦点* *,就表示该事件。然而,唠叨用户是因为这不是我认为好的UI设计,并且会刺激用户(我知道这会刺激我)很多。 –

+0

另外,失去焦点的窗口与正在关闭的窗口完全不同。您可能希望将标题更新为更适合的内容。 –

+0

我想在点击“确定”或“取消”按钮之前屏蔽屏幕。这对我的应用程序非常重要。这不是平常的应用。我有Web视图中它,如果用户要打开不同的程序(点击OK)我有发送请求到服务器(调用RESTful Web服务) – gogagubi

回答

1

对于近处的事件:在你的主窗口类

重新实现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) ; 
} 
+0

这个变体像我以前的变体一样工作。在用户点击“确定”或“取消”之前,仍然无法屏蔽alt +选项卡上的屏幕。 MessageBox保持背景。 – gogagubi

+0

它看起来像你应该描述你想以更容易理解的方式实现...要“激活”窗口,你可以尝试调用方法activateWindow()http://doc.qt.io/qt-5/qwidget.html #activateWindow或raise() –

+0

但正如我写的,它不清楚你最终想要达到什么样的行为 –