2016-02-02 63 views
0

我一直试图隐藏一个独立的对话框应用程序,当用户点击典型的关闭按钮(通常在最小化按钮旁边的角落中的X)I cam跨越这个帖子:Qt对话框X按钮覆盖拒绝按预期工作

Qt: How do I handle the event of the user pressing the 'X' (close) button?

我本以为这有我的解决方案,但是当我实现它,我得到奇怪的行为。

void MyDialog::reject() 
{ 
    this->hide() 
} 

当我打的X按钮整个应用程序关闭(进程消失),这是不是我想要的。由于我的gui产生了一个命令行,我设置了一个测试系统,我可以通过一个文本命令来告诉我的对话框隐藏,我调用相同的'this-> hide()'指令,并且一切正常。当我告诉它显示时,该对话框隐藏并显示备份。

任何想法为什么拒绝方法即使我没有明确告诉它完全关闭我的应用程序?

+0

当您关闭对话框时,应用程序的任何其他窗口是否可见? – peppe

回答

0

覆盖对话框类中的虚函数“virtual void closeEvent(QCloseEvent * e)”。代码评论将详细解释。

Dialog::Dialog(QWidget *parent) :QDialog(parent), ui(new Ui::Dialog){ 
    ui->setupUi(this); 
} 
Dialog::~Dialog(){ 
    delete ui; 
} 
//SLOT 
void Dialog::fnShow(){ 
    //Show the dialog 
    this->show(); 
} 
void Dialog::closeEvent(QCloseEvent *e){ 
    QMessageBox::StandardButton resBtn = QMessageBox::question(this, "APP_NAME", 
             tr("Are you sure?\n"), 
             QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes, 
             QMessageBox::Yes); 

    if (resBtn != QMessageBox::Yes){ 
     //Hiding the dialog when the close button clicked 
     this->hide(); 
     //event ignored 
     e->ignore(); 
     //Testing. To show the dialog again after 2 seconds 
     QTimer *qtimer = new QTimer(this); 
     qtimer->singleShot(2000,this,SLOT(fnShow())); 
     qtimer->deleteLater(); 
    } 
    //below code is for understanding 
    //as by default it is e->accept(); 
    else{ 
     //close forever 
     e->accept(); 
    } 
} 
+0

我之前遇到过这个,当我在QDialog上实现它时,只是使用了e-> ignore()行来进行快速测试,并且我的对话框停止了对X的点击响应。让我重试它并再次检查。 – MrJman006

+0

无论出于何种原因,现在适用于我。谢谢您的帮助! – MrJman006