【1】close 与 Qt::WA_DeleteOnClose简介

1.1 Qt源码

 /*!
Closes this widget. Returns \c true if the widget was closed;
otherwise returns \c false. First it sends the widget a QCloseEvent. The widget is
\l{hide()}{hidden} if it \l{QCloseEvent::accept()}{accepts}
the close event. If it \l{QCloseEvent::ignore()}{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 \l 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. */ bool QWidget::close()
{
return d_func()->close_helper(QWidgetPrivate::CloseWithEvent);
} /*!
This event handler is called with the given \a event when Qt receives a window
close request for a top-level widget from the window system. By default, the event is accepted and the widget is closed. You can reimplement
this function to change the way the widget responds to window close requests.
For example, you can prevent the window from closing by calling \l{QEvent::}{ignore()}
on all events. Main window applications typically use reimplementations of this function to check
whether the user's work has been saved and ask for permission before closing.
For example, the \l{Application Example} uses a helper function to determine whether
or not to close the window: \snippet mainwindows/application/mainwindow.cpp 3
\snippet mainwindows/application/mainwindow.cpp 4 \sa event(), hide(), close(), QCloseEvent, {Application Example}
*/ void QWidget::closeEvent(QCloseEvent *event)
{
event->accept();
}

1.2 公共槽函数

1.3 帮助文档

1.4 Qt::WA_DeleteOnClose

【2】实例代码

1.1 TWidget.h

 #ifndef TWIDGET_H
#define TWIDGET_H #include <QWidget>
#include <QDebug> namespace Ui
{
class TWidget;
} class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = NULL);
~MyWidget();
}; class TWidget : public QWidget
{
Q_OBJECT public:
explicit TWidget(QWidget *parent = );
~TWidget(); private slots:
void onPushButtonPressed(); private:
Ui::TWidget *m_pUI;
MyWidget *m_pMyWidget;
}; #endif // TWIDGET_H

1.2 TWidget.cpp

 #include "TWidget.h"
#include "ui_TWidget.h" MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
qDebug() << "construct :: MyWidget";
} MyWidget::~MyWidget()
{
qDebug() << "destruct :: ~MyWidget";
} TWidget::TWidget(QWidget *parent)
: QWidget(parent)
, m_pUI(new Ui::TWidget)
, m_pMyWidget(NULL)
{
m_pUI->setupUi(this); m_pMyWidget = new MyWidget();
m_pMyWidget->setFixedSize(, );
m_pMyWidget->setAttribute(Qt::WA_DeleteOnClose); // 设置属性Qt::WA_DeleteOnClose connect(m_pUI->pushButton, &QPushButton::pressed, this, &TWidget::onPushButtonPressed);
} TWidget::~TWidget()
{
if (m_pUI != NULL)
{
delete m_pUI;
m_pUI = NULL;
} if (m_pMyWidget != NULL)
{
delete m_pMyWidget;
m_pMyWidget = NULL;
}
} void TWidget::onPushButtonPressed()
{
if (m_pMyWidget != NULL)
{
m_pMyWidget->show();
}
}

1.3 main.cpp

 #include "TWidget.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv); TWidget w;
w.show(); return a.exec();
}

1.4 TWidget.ui 界面(UI很简单,仅仅为了验证问题,只放置了一个PushButton)

1.5 运行结果:

注意观察,现象如下:

第一次点击 pushButton 按钮,对话框弹出,关闭对话框。打印日志如下:

 construct :: MyWidget
destruct :: ~MyWidget

第二次点击 pushButton 按钮,程序崩溃。

1.6 注释掉设置属性行(即TWidget.cpp 第24行),再编译、运行、一切正常。

【3】总结

如果设置窗体的Qt::WA_DeleteOnClose属性:调用close方法时,窗体将被析构掉。

Good Good Study, Day Day Up.

顺序 选择 循环 总结

QWidget 的 close 与 Qt::WA_DeleteOnClose的更多相关文章

  1. Qt浅谈之一:内存泄露(总结),对于QWidget可以setAttribute(Qt::WA_DeleteOnClose),而且绝对不能手动删除栈上的对象

    一.简介 Qt内存管理机制:Qt 在内部能够维护对象的层次结构.对于可视元素,这种层次结构就是子组件与父组件的关系:对于非可视元素,则是一个对象与另一个对象的从属关系.在 Qt 中,在 Qt 中,删除 ...

  2. Qt 程序退出时断言错误——_BLOCK_TYPE_IS_VALID(pHead->nBlockUse),由setAttribute(Qt::WA_DeleteOnClose)引起

    最近在学习QT,自己仿写了一个简单的QT绘图程序,但是在退出时总是报错,断言错误: 报错主要问题在_BLOCK_TYPE_IS_VALID(pHead->nBlockUse),是在关闭窗口时报的 ...

  3. Qt 窗口属性简介之Qt::WA_DeleteOnClose

    一.简述 今天介绍一个简单的窗口属性——Qt::WA_DeleteOnClose. 在正常创建窗口后,我们一般会调用close()方法来关闭窗口,这里我们看一下Q助手中关于close()方法的介绍. ...

  4. Qt 窗体的模态与非模态(setWindowFlags(Qt::WindowStaysOnTopHint);比较有用,还有Qt::WA_DeleteOnClose)

    概念 模态对话框(Modal Dialog)与非模态对话框(Modeless Dialog)的概念不是Qt所独有的,在各种不同的平台下都存在.又有叫法是称为模式对话框,无模式对话框等. 1. 模态窗体 ...

  5. Qt::WA_DeleteOnClose 造成的野指针问题

    今天遇到了一个由Qt::WA_DeleteOnClose造成的野指针问题,在网上搜到的一个求助贴如下(http://bbs.csdn.net/topics/380182058): 主窗口类QMainW ...

  6. Qt的模态对话框和非模态对话框 经常使用setAttribute (Qt::WA_DeleteOnClose)

    模态对话框就是指在子对话框弹出时,焦点被强行集中于该子对话框,子对话框不关闭,用户将无法操作其他的窗口.非模态相反,用户仍然可以操作其他的窗口,包括该子对话框的父对话框. 如果从线程角度来讲,模态对话 ...

  7. QWidget居中显示(qt窗口坐标原点是在”左上角”的,有图)

    转载请说明出处, 并附上原文链接http://blog.csdn.net/qq907482638/article/details/72189014. 问题描述 在Qt学习过程中,在让QDialog居中 ...

  8. QT 随笔目录

    [1]基础部分 <信号和槽机制> <信号与槽知识点> <QString 与 string转换> <QT 继承QWidget && 继承QDia ...

  9. QT中QWidget类简介

    一.详细描述 QWidget类是所有用户界面对象的基类.通俗的来讲,Qt基本上所有的UI类都是由QWidget继承出来的,而QWidget继承于QObject,  大家可以查阅Qt source 即可 ...

随机推荐

  1. 使用github的srs代码,搭建 RTMP_Server

    1. 搭建RTMP服务器 1> 获取开源代码SRS. git clone https://github.com/ossrs/srs 下载源码后,按照如下文档安装https://github.co ...

  2. php 的函数

    一.函数定义及变量作用域 1. 函数的声明和调用 函数的目的是复用. [$variable=] function [name]([$param]){} 2. 变量的作用域 (1) 全局变量 函数内部想 ...

  3. 10.2-uC/OS-III内部任务管理(任务状态)

    1.任务状态 从用户的观点来看,任务可以是有 5种状态,见图 5-6.展示了任务状态间的转换关系. {休眠状态,就绪状态,运行状态,挂起状态,中断状态} (1).处于休眠状态的任务驻留于内存但未被uC ...

  4. LeetCode-188.Best Time to Buy and Sell Stock IV

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. python-面向对象-06_私有属性和私有方法

    私有属性和私有方法 01. 应用场景及定义方式 应用场景 在实际开发中,对象 的 某些属性或方法 可能只希望 在对象的内部被使用,而 不希望在外部被访问到 私有属性 就是 对象 不希望公开的 属性 私 ...

  6. thinkphp安装不成功可能跟数据库名有关

    今天ytkah在安装thinkphp时提示无法连接数据库,删除数据库重新连接不行,更新了mysql版本也不行,后面就干脆换一个数据库名居然可以了.之前的数据库名包含大写字母,就是因为这个问题才导致安装 ...

  7. php程序猿面试分享

    面试总结 今天去了北京著名IT公司进行PHP程序猿的面试.这是人生第一次么,怎么不紧张?我是不是有病.不是.这叫自信呵. 首先是做一些笔试题. 1.mysql数据库索引使用的数据结构?这样做的优点是? ...

  8. 数据库——MongoDB

    what's the MongoDB MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似json的bson格式 ...

  9. MySQL5.7配置基于GTID的复制

    MySQL5.7下配置GTID复制的方法: 修改主库和从库的配置文件,加入下列部分的配置项:主库:[mysqld]log-bin=mysql-binbinlog_format= ROWgtid-mod ...

  10. 常见Chrome 插件

    Chrome插件网:http://chromecj.com/downloadstart.html Chrome浏览器:http://chromecj.com/chrome/2014-09/177.ht ...