找回密码
 立即注册
收起左侧

把Mainwindow的closeEvent()重新编写为一个空函数,为什么还照样...

8
回复
9530
查看
[复制链接]

尚未签到

来源: 2013-12-18 14:01:27 显示全部楼层 |阅读模式
1Qter豆
把Mainwindow的closeEvent()事件处理器重新编写为一个空函数,为什么还照样能关闭窗口呢?
在我看来,closeEvent()事件处理器的工作就是把MainWindow窗口隐藏(或者销毁),但我重写定义了closeEvent(),函数里面什么也没写,没有任何代码.我想再点叉叉的时候肯定MainWindow窗口不会有任何反应,但没想到,窗口照样关闭了.这是为什么呢?难道我新定义的closeEvent()压根就没执行?

最佳答案

查看完整内容

有些问题是可以找文档解决的。。。请按f1 By default, the event is accepted and the widget is closed. You can reimplement this function to change the way the widget responds to window close requests. For example, you can prevent the window from closing by calling ignore() on all events.
回复

使用道具 举报

尚未签到

2013-12-18 14:01:28 显示全部楼层
有些问题是可以找文档解决的。。。请按f1

By default, the event is accepted and the widget is closed. You can reimplement this function to change the way the widget responds to window close requests. For example, you can prevent the window from closing by calling ignore() on all events.
  1. void MainWindow::closeEvent(QCloseEvent *event)
  2. {
  3.     if (maybeSave()) {
  4.         writeSettings();
  5.         event->accept();
  6.     } else {
  7.         event->ignore();
  8.     }
  9. }
复制代码
回复

使用道具 举报

尚未签到

2013-12-18 15:31:39 显示全部楼层
Joey_Chan 发表于 2013-12-18 14:43
有些问题是可以找文档解决的。。。请按f1

By default, the event is accepted and the widget is closed.  ...

不是F1的问题,我早就知道用ignore()可以使关窗口无效.
我的问题是,既然我已经重写了closeEvent()函数,并且,里面没有任何代码,不可能操作MainWindow了,可是我按叉叉MainWindow照样关闭.这是为什么
回复

使用道具 举报

尚未签到

2013-12-18 15:37:38 显示全部楼层
ll5152003 发表于 2013-12-18 15:31
不是F1的问题,我早就知道用ignore()可以使关窗口无效.
我的问题是,既然我已经重写了closeEvent()函数,并 ...

我不是回答了你的问题吗???

By default, the event is accepted and the widget is closed.

拜托请看文档仔细点,就算你什么都不写默认就是accepted
回复

使用道具 举报

尚未签到

2013-12-18 15:41:49 显示全部楼层
本帖最后由 ll5152003 于 2013-12-18 16:01 编辑
Joey_Chan 发表于 2013-12-18 15:37
我不是回答了你的问题吗???

By default, the event is accepted and the widget is closed.

这个default难道不是在closeEvent()里定义的吗???我都把closeEvent()设为空了,这个default还存在吗?还起作用吗?
再说,就是再default,它关闭也得用closeEvent()来关啊,我现在都已经把closeEvent()改写为空了,它拿什么来关呢?
回复

使用道具 举报

尚未签到

2013-12-18 16:14:14 显示全部楼层
ll5152003 发表于 2013-12-18 15:41
这个default难道不是在closeEvent()里定义的吗???我都把closeEvent()设为空了,这个default还存在吗?还起作 ...

那我没法回答了,请你看Qt的源码吧
回复

使用道具 举报

尚未签到

2013-12-18 17:00:25 显示全部楼层
Joey_Chan 发表于 2013-12-18 16:14
那我没法回答了,请你看Qt的源码吧

还是非常感谢,这么热心的帮我解答,谢谢
回复

使用道具 举报

尚未签到

2013-12-18 17:39:51 显示全部楼层
ll5152003 发表于 2013-12-18 17:00
还是非常感谢,这么热心的帮我解答,谢谢

我没看过qt的源码,估计QEven都会有一个默认的处理方法
回复

使用道具 举报

累计签到:6 天
连续签到:1 天
2013-12-18 22:24:25 显示全部楼层
LZ。。你没处理过event, 而是留空了,显然,event就去执行default了,
加一句event->ignore();即可
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了