close详解

1 详解

bool QWidget::close() [slot]
Closes this widget. Returns true if the widget was closed; otherwise returns false.

First it sends the widget a QCloseEvent. The widget is hidden if it accepts the close event. If it ignores the event, nothing happens. The default implementation of QWidget::closeEvent() accepts the close event.

If the widget has the Qt::WA_DeleteOnClose flag, the widget is also deleted. A close events is delivered to the widget no matter if the widget is visible or not.

The QApplication::lastWindowClosed() signal is emitted when the last visible primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose attribute set is closed. By default this attribute is set for all widgets except transient windows such as splash screens, tool windows, and popup menus.

上面是qt assistant关于QWidget类的close槽的说明。

关闭widget。如果widget被关闭返回true,否则返回false。
它首先向widget发送QCloseEvent事件。如果窗体接收到close事件就隐藏。如果它忽略了该事件,就什么都不会发生。QWidget::closeEvent()的默认实现接收close事件。
如果widget有Qt::WA_DeleteOnClose标志,则widget也会被删除。无论widget是否可见close事件都会投递到widget。
当最后一个带有Qt::WA_QuitOnClose属性的可见主窗口被关闭时,会发送QApplication::lastWindowClosed()信号。该属性默认所有widget都有,除了splash screens, tool windows, and popup menus这些临时窗体。

void QWidget::hide() [slot]
Hides the widget. This function is equivalent to setVisible(false).
隐藏widget。该函数等同于 setVisible(false)。

那么close和hide区别就很明显了:
hide只是隐藏窗体。不会发送任何信号。
close一般也是隐藏窗口。但是它会发送QCloseEvent事件。你可以重写void QWidget::closeEvent(QCloseEvent * event) [virtual protected],可以隐藏widget或者不隐藏。Qt::WA_DeleteOnClose标志还会影响窗体在内存中的状态,如果设置了该标志,窗体就会被删除,而hide则不会。最后主窗体的close会导致整个程序的退出,而hide明显不会。

PS:再对一个窗体调用close函数后,如果再调用show(),这个窗体又会被显示出来。

2、关闭窗口与终止程序
一个(应用)程序通常拥有多个窗口,关闭(或删除)一个窗口,并不一定会使程序终止,Qt中关闭窗口使用QWidget::close()槽函数,终止程序使用的是QCoreApplication::quit()静态槽函数或QCoreApplication::exit()静态函数

3、与关闭部件和终止程序有关的属性
注:以下属性其实是Qt::WidgetAttribute枚举的成员,可使用QWidget::setAttribute()函数进行设置和清除。
①、Qt::WA_DeleteOnClose属性:表示当部件接受到QCloseEvent事件时,是否让Qt删除部件。若该属性为true,则删除部件,否则部件只是隐藏。注意:设置了该属性的部件需要使用new创建,否则会产生内存错误。
②、Qt::WA_QuitOnClose属性:表示当拥有该属性的最后一个部件接受到QCloseEvent事件时,让Qt终止应用程序。默认情况下,所有Qt::Window类型的部件都具有该属性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

季截

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值