QWidget 的 close 与 Qt::WA_DeleteOnClose

摘要:
ignore()}{忽略}8事件:close()24{25returnd_func()->close_helper(QWidgetPrivate:accept():publicQ:

【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 公共槽函数

QWidget 的 close 与 Qt::WA_DeleteOnClose第1张

1.3 帮助文档

QWidget 的 close 与 Qt::WA_DeleteOnClose第2张

1.4 Qt::WA_DeleteOnClose

QWidget 的 close 与 Qt::WA_DeleteOnClose第3张

【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)

QWidget 的 close 与 Qt::WA_DeleteOnClose第4张

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.

顺序 选择 循环 总结

免责声明:文章转载自《QWidget 的 close 与 Qt::WA_DeleteOnClose》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇[Linux] Linux C编程一站式学习 Part.1erase-credentials配置下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

unittest自定义封装与应用

和大家分享一个自己二次封装uniitest的方法,大家可以在评论区多多指教,一起分享学习; 一、unittest基类封装 import osimport unittestfrom common.log_print import Logfrom common.get_config import getconfigfrom common.base_page_i...

three车辆自由转弯(vue 极品飞车)

//最近没有时间整理代码,就这样吧 <template> <div> <div id="map"></div> </div> </template> <script> // import * as Three from '../../node_modules/three/b...

Hutool-解析JSON

1、创建JSONObject JSONObject jsonObject = JSONUtil.createObj() .put("姓名","张三") .put("年龄",12) .put("国籍","中国") .put("爱好"...

基于jQuery美化联动下拉选择框

今天给大家介绍一款基于jQuery美化联动下拉选择框。这款下下拉选择框js里自带了全国所有城市的数数库。下拉选择框适用浏览器:IE8、360、FireFox、Chrome、Safari、Opera、傲游、搜狗、世界之窗。效果图如下: 在线预览   源码下载 实现的代码。 html代码: <div id="container">...

博客迁移到亚马逊云端1

将我的博客迁移到亚马逊云端(1)  Octopress已经被公认为Geeker的博客框架。它所拥有的特性都很符合Geeker的癖好:强大的命令行操作方式、简洁的MarkDown语法、灵活的插件配置、美轮美奂的theme(自带响应式设计哦)、完全可定义的部署…… 一般大家都喜欢把博客部署到github pages上,免费速度快,与Octopress无缝...

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?> <!-- CacheManager Configuration ========================== An ehcache.xml corresponds to a single CacheManager. See instructions...