事情是这样的:今天调试窗体,突然发现按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();
}
 
 
http://www.cnblogs.com/codingmylife/archive/2010/04/29/1724571.html

最新文章

  1. 判断一个对象是jQuery对象还是DOM对象
  2. POJ 2096 Collecting Bugs (概率DP)
  3. Loadrunner的字符串函数
  4. Redhat 6.4_联网 yum 配置
  5. 使用动态SQL语句实现简单的行列转置(动态产生列)
  6. MAC 开发工具
  7. IPTV小窗口播放视频 页面焦点无法移动的解决方法
  8. AspNet.WebAPI.OData.ODataPQ
  9. Managed Switch: Confs
  10. jquery 改变变量出现值不同步
  11. 官方 React 快速上手脚手架 create-react-app
  12. 【转】HTTP Header 详解
  13. python的流程控制学习
  14. 监控 Linux 性能的 18 个命令行工具[转]
  15. Vue 部署IIS 单页面刷新报404问题
  16. rand和srand的用法
  17. B1018. 锤子剪刀布
  18. 三篇文章带你极速入门php(一)之语法
  19. Web Services的学习一
  20. 有趣的NaN类型

热门文章

  1. 【25.47%】【codeforces 733D】Kostya the Sculptor
  2. flex新的心得
  3. android studio 各种问题 应该能帮助到你们
  4. Project Euler:Problem 39 Integer right triangles
  5. Spring中的Interceptor 拦截器 专题
  6. R 语言学习(二)—— 向量
  7. Etag & If-None-Match 专题
  8. 开发Android项目中使用androidannotations
  9. 一些自成系统、完备的教程(链接、博客、github等)
  10. Analysis of variance(ANOVA)