Qt按ESC关闭模态对话框不触发closeEvent()问题解析

      事情是这样的:今天调试窗体,突然发现按ESC键居然跳过closeEvent()关闭了对话框!我的关闭判断都在closeEvent()里,这直接导致非正常关闭了正在进行的工作。先重建下场景:

调用处:

{
    Corpus corpusDialog(this);
    corpusDialog.exec();
}

典型的模态对话框,因为用了exec()。Corpus对话框类中覆盖了closeEvent(),其中父类的closeEvent()为虚函数。这样,当我按ESC关闭对话框就出现了如题情景。

后来发现,居然是因为Corpus类中没有覆盖reject()函数导致的!由Qt官方文档:

Escape Key

If the user presses the Esc key in a dialog, QDialog::reject() will be called. This will cause the window to close: The close event cannot be ignored.

得知按ESC默认调用的是reject()函数。由于Corpus类中没有覆盖,直接调用了父类QDialog的reject()。调用QDialog的reject()会发生什么呢?

它实际调用了done(QDialog::Reject)(同样由于没有被覆盖,调用的是QDialog的done)。由Qt官方文档:

void QDialog::done ( int r[virtual slot]

Closes the dialog and sets its result code to r. If this dialog is shown withexec(), done() causes the local event loop to finish, and exec() to return r.

看不出done()到底做了些什么。调试跟踪了一圈,发现就我这种情况来讲

  • done(QDialog::Reject)仅hide了对话框,
  • 然后设置了exec()返回值为QDialog::Reject,
  • 关闭窗体事件循环(这个看不出,估计的),
  • 释放finished(QDialog::Reject)信号,
  • 最后释放rejected()信号。

      的却没有关闭窗体呀,所以也不会触发closeEvent()。我应当在Corpus类中覆盖掉reject(),由reject()调用close()。这样就把问题解决了!

void Corpus::reject()
{
    close();
}
posted @ 2010-04-29 23:53  紫红的泪  阅读(9524)  评论(4编辑  收藏  举报