qt窗体close和hide的区别

如题所述

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明显不会。
温馨提示:答案为网友推荐,仅供参考
相似回答