Qt按ESC关闭模态对话框不触发closeEvent()问题解析(ESC默认调用的是reject()函数,所以必须覆盖这个函数才会有效果)good
事情是这样的:今天调试窗体,突然发现按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();
}
最新文章
- 判断一个对象是jQuery对象还是DOM对象
- POJ 2096 Collecting Bugs (概率DP)
- Loadrunner的字符串函数
- Redhat 6.4_联网 yum 配置
- 使用动态SQL语句实现简单的行列转置(动态产生列)
- MAC 开发工具
- IPTV小窗口播放视频 页面焦点无法移动的解决方法
- AspNet.WebAPI.OData.ODataPQ
- Managed Switch: Confs
- jquery 改变变量出现值不同步
- 官方 React 快速上手脚手架 create-react-app
- 【转】HTTP Header 详解
- python的流程控制学习
- 监控 Linux 性能的 18 个命令行工具[转]
- Vue 部署IIS 单页面刷新报404问题
- rand和srand的用法
- B1018. 锤子剪刀布
- 三篇文章带你极速入门php(一)之语法
- Web Services的学习一
- 有趣的NaN类型
热门文章
- 【25.47%】【codeforces 733D】Kostya the Sculptor
- flex新的心得
- android studio 各种问题 应该能帮助到你们
- Project Euler:Problem 39 Integer right triangles
- Spring中的Interceptor 拦截器 专题
- R 语言学习(二)—— 向量
- Etag &; If-None-Match 专题
- 开发Android项目中使用androidannotations
- 一些自成系统、完备的教程(链接、博客、github等)
- Analysis of variance(ANOVA)