qt窗口关闭退出程序_Qt5 窗口关闭信号的响应~
有時候我們會有這樣的需求:
窗口關閉信號觸發之前響應一些什么東西~
需要用到QCloseEvent
#include
The QCloseEvent class contains parameters that describe a close event.
Close events are sent to widgets that the user wants to close, usually by choosing "Close" from the window menu, or by clicking the X title bar button. They are also sent when you call QWidget::close() to close a widget programmatically.
Close events contain a flag that indicates whether the receiver wants the widget to be closed or not. When a widget accepts the close event, it is hidden (and destroyed if it was created with the Qt::WA_DeleteOnClose flag). If it refuses to accept the close event nothing happens. (Under X11 it is possible that the window manager will forcibly close the window; but at the time of writing we are not aware of any window manager that does this.)
The event handler QWidget::closeEvent() receives close events. The default implementation of this event handler accepts the close event. If you do not want your widget to be hidden, or want some special handing, you should reimplement the event handler and ignore() the event.
The closeEvent() in the Application example shows a close event handler that asks whether to save a document before closing.
If you want the widget to be deleted when it is closed, create it with the Qt::WA_DeleteOnClose flag. This is very useful for independent top-level windows in a multi-window application.
QObjects emits the destroyed() signal when they are deleted.
If the last top-level window is closed, the QGuiApplication::lastWindowClosed() signal is emitted.
The isAccepted() function returns true if the event's receiver has agreed to close the widget; call accept() to agree to close the widget and call ignore() if the receiver of this event does not want the widget to be closed.
See also QWidget::close(), QWidget::hide(), QObject::destroyed(), QCoreApplication::exec(), QCoreApplication::quit(), and QGuiApplication::lastWindowClosed().重寫QWidget的虛函數
protected:
void closeEvent(QCloseEvent *event);
void MainWindow::closeEvent(QCloseEvent *event)
{
//|窗口關閉之前需要的操作~
}
總結
以上是生活随笔為你收集整理的qt窗口关闭退出程序_Qt5 窗口关闭信号的响应~的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 照片拼接长图_我才发现,
- 下一篇: 原理分析_生化分析仪原理结构及参数对比