【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.
顺序 选择 循环 总结