【1】close 与 Qt::WA_DeleteOnClose简介
1.1 Qt源码
1 /*! 2 Closes this widget. Returns c true if the widget was closed; 3 otherwise returns c false. 4 5 First it sends the widget a QCloseEvent. The widget is 6 l{hide()}{hidden} if it l{QCloseEvent::accept()}{accepts} 7 the close event. If it l{QCloseEvent::ignore()}{ignores} 8 the event, nothing happens. The default 9 implementation of QWidget::closeEvent() accepts the close event. 10 11 If the widget has the Qt::WA_DeleteOnClose flag, the widget 12 is also deleted. A close events is delivered to the widget no 13 matter if the widget is visible or not. 14 15 The l QApplication::lastWindowClosed() signal is emitted when the 16 last visible primary window (i.e. window with no parent) with the 17 Qt::WA_QuitOnClose attribute set is closed. By default this 18 attribute is set for all widgets except transient windows such as 19 splash screens, tool windows, and popup menus. 20 21 */ 22 23 bool QWidget::close() 24 { 25 return d_func()->close_helper(QWidgetPrivate::CloseWithEvent); 26 } 27 28 /*! 29 This event handler is called with the given a event when Qt receives a window 30 close request for a top-level widget from the window system. 31 32 By default, the event is accepted and the widget is closed. You can reimplement 33 this function to change the way the widget responds to window close requests. 34 For example, you can prevent the window from closing by calling l{QEvent::}{ignore()} 35 on all events. 36 37 Main window applications typically use reimplementations of this function to check 38 whether the user's work has been saved and ask for permission before closing. 39 For example, the l{Application Example} uses a helper function to determine whether 40 or not to close the window: 41 42 snippet mainwindows/application/mainwindow.cpp 3 43 snippet mainwindows/application/mainwindow.cpp 4 44 45 sa event(), hide(), close(), QCloseEvent, {Application Example} 46 */ 47 48 void QWidget::closeEvent(QCloseEvent *event) 49 { 50 event->accept(); 51 }
1.2 公共槽函数
1.3 帮助文档
1.4 Qt::WA_DeleteOnClose
【2】实例代码
1.1 TWidget.h
1 #ifndef TWIDGET_H 2 #define TWIDGET_H 3 4 #include <QWidget> 5 #include <QDebug> 6 7 namespace Ui 8 { 9 class TWidget; 10 } 11 12 class MyWidget : public QWidget 13 { 14 public: 15 MyWidget(QWidget *parent = NULL); 16 ~MyWidget(); 17 }; 18 19 class TWidget : public QWidget 20 { 21 Q_OBJECT 22 23 public: 24 explicit TWidget(QWidget *parent = 0); 25 ~TWidget(); 26 27 private slots: 28 void onPushButtonPressed(); 29 30 private: 31 Ui::TWidget *m_pUI; 32 MyWidget *m_pMyWidget; 33 }; 34 35 #endif // TWIDGET_H
1.2 TWidget.cpp
1 #include "TWidget.h" 2 #include "ui_TWidget.h" 3 4 MyWidget::MyWidget(QWidget *parent) 5 : QWidget(parent) 6 { 7 qDebug() << "construct :: MyWidget"; 8 } 9 10 MyWidget::~MyWidget() 11 { 12 qDebug() << "destruct :: ~MyWidget"; 13 } 14 15 TWidget::TWidget(QWidget *parent) 16 : QWidget(parent) 17 , m_pUI(new Ui::TWidget) 18 , m_pMyWidget(NULL) 19 { 20 m_pUI->setupUi(this); 21 22 m_pMyWidget = new MyWidget(); 23 m_pMyWidget->setFixedSize(200, 200); 24 m_pMyWidget->setAttribute(Qt::WA_DeleteOnClose); // 设置属性Qt::WA_DeleteOnClose 25 26 connect(m_pUI->pushButton, &QPushButton::pressed, this, &TWidget::onPushButtonPressed); 27 } 28 29 TWidget::~TWidget() 30 { 31 if (m_pUI != NULL) 32 { 33 delete m_pUI; 34 m_pUI = NULL; 35 } 36 37 if (m_pMyWidget != NULL) 38 { 39 delete m_pMyWidget; 40 m_pMyWidget = NULL; 41 } 42 } 43 44 void TWidget::onPushButtonPressed() 45 { 46 if (m_pMyWidget != NULL) 47 { 48 m_pMyWidget->show(); 49 } 50 }
1.3 main.cpp
1 #include "TWidget.h" 2 #include <QApplication> 3 4 int main(int argc, char *argv[]) 5 { 6 QApplication a(argc, argv); 7 8 TWidget w; 9 w.show(); 10 11 return a.exec(); 12 }
1.4 TWidget.ui 界面(UI很简单,仅仅为了验证问题,只放置了一个PushButton)
1.5 运行结果:
注意观察,现象如下:
第一次点击 pushButton 按钮,对话框弹出,关闭对话框。打印日志如下:
1 construct :: MyWidget 2 destruct :: ~MyWidget
第二次点击 pushButton 按钮,程序崩溃。
1.6 注释掉设置属性行(即TWidget.cpp 第24行),再编译、运行、一切正常。
【3】总结
如果设置窗体的Qt::WA_DeleteOnClose属性:调用close方法时,窗体将被析构掉。
Good Good Study, Day Day Up.
顺序 选择 循环 总结