Qt销毁非模态对话框
2014-05-29 00:41
633 查看
很多时候需要非模态对话框:当关闭主窗口时,往往非模态对话框依然存在。
bool QWidget::close ()
Closes this widget. Returns true if the widget was closed; otherwise returns false.
First it sends the widget a QCloseEvent. The widget is hidden if it accepts the close event. If it 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 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.
由以上可以通过QPointer,在closeEvent中delete掉非模态对话框,如下:
ParentWidget窗体类中有一个QPointer<ChildWidget> m_pChildWidget;子窗体。
ParentWidget::ParentWidget(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags),m_pChildWidget(NULL)
{
ui.setupUi(this);
connect(ui.pushButton,SIGNAL(clicked()),this,SLOT(createWidget()));
}
ParentWidget::~ParentWidget()
{
}
void ParentWidget::createWidget()
{
if(m_pChildWidget)
{
qDebug()<<"m_pChildWidget != NULL";
m_pChildWidget->showNormal();
m_pChildWidget->activateWindow();
}
else
{
qDebug()<<"m_pChildWidget == NULL";
m_pChildWidget = new ChildWidget;
m_pChildWidget->show();
}
}
void ParentWidget::closeEvent( QCloseEvent *ev )
{
qDebug()<<m_pChildWidget;
delete m_pChildWidget;
}
ChildWidget可以设置setAttribute (Qt::WA_DeleteOnClose);属性,当close时就delete widget。QPointer指针在对象被销毁时会自动被设置为NULL。以下是QPointer的Assistant中的介绍:
The QPointer class is a template class that provides guarded pointers to QObject.
A guarded pointer, QPointer<T>, behaves like a normal C++ pointer T *, except that it is automatically set to 0 when the referenced object is destroyed (unlike normal C++ pointers, which
become "dangling pointers" in such cases). T must be a subclass of QObject.
Guarded pointers are useful whenever you need to store a pointer to a QObject that is owned by someone else, and therefore might be destroyed while you still hold a reference to it.
You can safely test the pointer for validity.
QPointer<QLabel> label = new QLabel;
label->setText("&Status:");
...
if (label)
label->show();
bool QWidget::close ()
Closes this widget. Returns true if the widget was closed; otherwise returns false.
First it sends the widget a QCloseEvent. The widget is hidden if it accepts the close event. If it 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 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.
由以上可以通过QPointer,在closeEvent中delete掉非模态对话框,如下:
ParentWidget窗体类中有一个QPointer<ChildWidget> m_pChildWidget;子窗体。
ParentWidget::ParentWidget(QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags),m_pChildWidget(NULL)
{
ui.setupUi(this);
connect(ui.pushButton,SIGNAL(clicked()),this,SLOT(createWidget()));
}
ParentWidget::~ParentWidget()
{
}
void ParentWidget::createWidget()
{
if(m_pChildWidget)
{
qDebug()<<"m_pChildWidget != NULL";
m_pChildWidget->showNormal();
m_pChildWidget->activateWindow();
}
else
{
qDebug()<<"m_pChildWidget == NULL";
m_pChildWidget = new ChildWidget;
m_pChildWidget->show();
}
}
void ParentWidget::closeEvent( QCloseEvent *ev )
{
qDebug()<<m_pChildWidget;
delete m_pChildWidget;
}
ChildWidget可以设置setAttribute (Qt::WA_DeleteOnClose);属性,当close时就delete widget。QPointer指针在对象被销毁时会自动被设置为NULL。以下是QPointer的Assistant中的介绍:
The QPointer class is a template class that provides guarded pointers to QObject.
A guarded pointer, QPointer<T>, behaves like a normal C++ pointer T *, except that it is automatically set to 0 when the referenced object is destroyed (unlike normal C++ pointers, which
become "dangling pointers" in such cases). T must be a subclass of QObject.
Guarded pointers are useful whenever you need to store a pointer to a QObject that is owned by someone else, and therefore might be destroyed while you still hold a reference to it.
You can safely test the pointer for validity.
QPointer<QLabel> label = new QLabel;
label->setText("&Status:");
...
if (label)
label->show();
相关文章推荐
- 再谈非模态对话框的销毁
- 非模态对话框的创建与销毁
- Qt 模态对话框与非模态对话框及生命周期
- Qt窗体关闭时,如何自动销毁窗体类对象
- Qt下的模态和非模态对话框
- Qt 销毁机制个人理解~
- QT模态对话框及非模态对话框
- Qt窗体关闭时,如何自动销毁窗体类对象
- 非模态对话框的销毁
- 再谈非模态对话框的销毁 / 对话框建立过程/ 对话框销毁过程/ 消息传递过程
- Qt下的模态与非模态对话框
- 再谈非模态对话框的销毁 / 对话框建立过程/ 对话框销毁过程/ 消息传递过程
- QT 模态对话框和非模态对话框
- VC非模态对话框创建和销毁
- QT 模态与非模态对话框的创建与退出
- MFC 非模态对话框覆盖主对话框销毁问题
- 非模态对话框 建立 销毁
- C++非模态对话框自动销毁的好方法
- VC非模态对话框创建和销毁
- 非模态对话框的销毁