PDA

View Full Version : Why doesn't my dialog window close when the mainwindow is closed?



pir
23rd July 2006, 13:58
Hi!

I have a problem with a dialog window that doesn't close when I close the MainWindow. The dialog isn't modal, because I want a window at the side of my main window that I can display state of the progam.

I've read about parents and childs and thought that a child is closed when it's parent is. So either I've got this wrong or I've failed making the dialog a child to the main window.

Heres a shortened version of the code:


This is the dialog class:


class OutlinerEditor : public QDialog
{
Q_OBJECT


public:

OutlinerEditor ( std::vector<Renderable*> *v,
std::vector<Renderable*> *s,
QWidget *parent ):
QDialog ( parent )
{
setWindowTitle ( "Outliner" );

treeModel = new TreeModel ( cmps );

outliner = new Outliner ( sels, this );
outliner->setModel ( treeModel );
outliner->connectSelection ( );

hLayout = new QHBoxLayout ( this );
hLayout->addWidget ( outliner );

hLayout->setMargin ( 1 );
setContentsMargins ( 1, 1, 1, 1 );

// and connections and so forth...

}// OutlinerEditor(v*,v*,W*)

};// class OutlinerEditor



This is the MainWindow class:



class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow::MainWindow ( ):
QMainWindow ( )
{
canvasFrame = new CanvasFrame;

/* This is where I build the dialog and connects it, the last argument is the
* pointer to the parent.
*/
OutlinerEditor *outlinerEditor = new OutlinerEditor ( NULL, NULL, this );

/* This is how I show the dialog as not modal */
outlinerEditor->show ( );

}// MainWindow()

};// class MainWindow



thanks for reading
pir

jpn
23rd July 2006, 14:15
Try to set attribute (http://doc.trolltech.com/4.1/qwidget.html#setAttribute) Qt::WA_DeleteOnClose for the main window. This assures that the main window is deleted upon closing and so the dialog should be destructed also.

pir
23rd July 2006, 15:21
I thought that the MainWindow was set to this as default. Thanks, I will try that. It's good to know, but I solved the problem with implementing the closeEvent() and close down all dialogs there. I needed to save the state of the dialogs anyway...

But is the parent child relation ok?
Because I'm a bit unsure about how the destruction of the widgets work. Can't find it explained in detail on the homepage. According to what I've understaned , is the children automatically destructed when the parent is destructed. But what about orphins? Do I need to destroy them myself, or does Qt's main loop take care of this when I close down the Qt session?

thanks
pir

wysota
23rd July 2006, 18:02
Because I'm a bit unsure about how the destruction of the widgets work.
If a QObject is destructed, it destroys all its children each of which in turn destroy all its children, etc.


But what about orphins? Do I need to destroy them myself, or does Qt's main loop take care of this when I close down the Qt session?
You have to destroy them yourself. Qt doesn't do anything *magic*, it's just a C++ library.