[SOLVED] How to destroy widget when close QDockWidget
Hi All!
I Have QMainWindow, at this main window, i add QDockWidget, to this dock widget, i add some widget, when i close dockWidget (push “X” at user interface), widget don’t call destructor, why? I think DockWidget not destroyed when user click to close button but set visibility false to this DockWidget. I’m right? And if i won’t to clear memory, i need connect to
- void visibilityChanged ( bool visible )
my code:
- inf->setWidget(new InformationDialog(m));
12 replies
When i set
window show like dialog in main window;
inf->setWidget(new InformationDialog(inf));
Not sure I underastand what you mean by this. Does it make a difference what you pass as parent?
Set attribute Qt::WA_DeleteOnClose at InformationDialog – nothing happen :(
But you want to set it on the dock widget, inf. That’s the one that can be closed by the user.
I have tried it on my Qt Creator. DockWidget is not deleted when we close it. Dock widget is deleted only when we close the application. So, your InformationDialog is deleted when you close the application ( InformationDialog widget becomes child of your DockWidget when you call setWidget() ).
So the question is do you want to delete QDockWidget when you close it , or you want to delete only InformationDialog??
Sorry for my earlier post, I have never used QMainWindow or QDockWidget. I program for Symbain.
Then you can do one think.. call a slot when visibilityChanged signal of QDocWidget is called and inside that you can delete QDocWidget if visibility == false;
this is how my sample implementation is ..
DockWidget.h
- #ifndef DOCKWIDGET_H
- #define DOCKWIDGET_H
- #include <QDockWidget>
- {
- Q_OBJECT
- public:
- {
- }
- public slots:
- void visibilityChanged1(bool visiblity);
- public:
- ~DockClass() {
- int a = 0;
- a++; // just to see that this destructor is called.
- }
- };
- #endif // DOCKWIDGET_H
my main.cpp
- #include <QPushButton>
- #include <QMainWindow>
- #include <QtGui/QApplication>
- #include <QDockWidget>
- #include "DockWidget.h"
- void DockClass::visibilityChanged1(bool visiblity) {
- if(!visiblity)
- deleteLater(); // this will delete DocClass which intern will delete buttonClass also.
- }
- int main(int argc, char *argv[])
- {
- QMainWindow mainWindow;
- DockClass * dockWidget = new DockClass();
- QObject::connect(dockWidget,SIGNAL(visibilityChanged(bool)),dockWidget,SLOT(visibilityChanged1(bool)));
- mainWindow.setCentralWidget(dockWidget);
- dockWidget->setWidget(buttonClass);
- mainWindow.show();
- return app.exec();
- }
Tnx all for help, I do that:
- void SomeClass::onInformation(){
- while(m != NULL){
- if(str == "QMainWindow"){
- break;
- }
- m = m->parent();
- }
- if(m == NULL)
- return;
- inf->setWidget(new InformationWidget(inf));
- connect(inf, SIGNAL(visibilityChanged(bool)), this, SLOT(onVisibilityChangedInformaionWidget(bool)));
- }
- //--------------------------------------------------------------------------------------------------
- void SomeClass::onVisibilityChangedInformaionWidget( bool visibility ){
- while(m != NULL){
- if(str == "QMainWindow"){
- break;
- }
- m = m->parent();
- }
- if(m == NULL)
- return;
- if(dock == NULL)
- return;
- m->removeDockWidget(dock);
- dock->deleteLater();
- }
You must log in to post a reply. Not a member yet? Register here!



