QWidget 的 close 与 Qt::WA_DeleteOnClose
Qt CLOSE QWidget
2023-09-14 09:12:19 时间
【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.
顺序 选择 循环 总结
相关文章
- Qt-Qt使用Mask(遮罩)实现异形按钮
- QT-Qt图片按钮类
- QT-Qt显示GIF图片
- 【C/C++学院】(17)QT标准对话框/toolbox类
- s3c2410上搭建QT/Embedded4.8.5开发环境(四)--安装intel-x86 X11平台qt库qt-everywhere-opensource-src-4.8.5
- Qt编写安防视频监控系统9-自动隐藏光标
- Qt编写物联网管理平台28-模拟工具
- qt 5.15 model如何改变欧拉旋转的旋转点
- Qt界面切换
- Qt QCheckBox QRadioButton
- Qt如何实现图形裁减
- Qt串口通信接收数据不完整_qt串口接收数据
- qt操作json(QT 教程)
- QT中槽的使用
- 【Qt】解决ubuntu中qt从一个工程去找另一个工程文件
- 9、QT基础——Qt消息机制和事件
- 3、 QT基础——第一个Qt小程序
- Qt 让Label显示图片并把图片居中
- Qt 时间获取
- Qt按钮透明后,长按出现黑色背景的解决方案
- 解决Ubuntu系统安装QT出错的问题:qt.qpa.plugin: Could not load the Qt platform plugin “xcb“ in ““ even though it
- vs2015中无法设置Qt版本,qt project setting 是灰色,Qt无法编译ui文件