[Solved] Closing dialogs doesn't work when mainwindow is closed
-
Hi,
I have 3 classes in use:
- MainWindow: This is my main application and is being showed when my program starts. It got a menubar with some functionality on it.
- Controller: When a functionality is clicked a controller will be opened. When this controller is being constructed it call Dialog to show a dialog to the user.
- Dialog
The problem is that when I click on functionality A, the dialog of A will be shown. However if I close my window now by pressing 'x' my dialog won't close.
Here are some codesnapshots
controller.h
@
class Controller : public QObject {
Q_OBJECTpublic:
Controller();
virtual ~Controller();
@
I have chosen for QObject since the controller contains some slots and thougt this is the most general type that fit the idea of the controller.controller.cpp
@
Controller::Controller() {
Dialog d = new Dialog(this);
d->show();
}
@mainwindow.cpp
@
Controller *controller = new Controller();
@Don't think the code of dialog is interesting since it only places some widgets on the dialog and calls for the controller when a signal is clicked.
Thank you in advance for helping me out.
-
Since the controller object is the parent for the Dialog instance i assume that you forgot to delete it in the mainwindow destructor (an alternative is to use the parent-child relationship to give the controller a parent - the mainwindow instance)
-
I think your Controller object has to be in any relationship to your mainwindow object. you should try to set mainwindow as parent of your controller object.
-
I tried to add the parent-child relationship to the controller and the mainWindow. but it didnt solved the problem. Altho i am not shure if i implemented the parent-child relationship well.
controller.h
@
class Controller : public QObject {
Q_OBJECTpublic:
Controller(QWidget*);
virtual ~Controller();
@
controller.cpp
@
Controller::Controller(QWidget *p) :QObject(p) {
Dialog d = new Dialog(this);
d->show();
}
@
mainwindow.cpp
@
Controller *controller = new Controller(this);
@ -
That should work, the Dialog class takes the controller parent? QDialog expects a QWidget* as a parent, make sure your error isn't there
Check with the debugger to see where the "chain" fails.
Or another method will be to set the Controller's parent as a parent to the Dialog too:
@
Controller::Controller(QWidget *p) :QObject(p) {
Dialog d = new Dialog(p); //pass p as a parent for the dialog
//since p should be the pointer to mainwindow instance
d->show();
}
@ -
This relationship will not help, as Controller is no widget. So the dialog will be a top level dialog.
Event the controller object will be destroyed when the main window object is destroyed, not when the main window is closed. The mainwindow object will be destroyed, when the event loop is exited and main exits :-)If MainWindow would be the parent of the dialog, it should work
-
Thank you Gerolf, this worked. I give now my Dialog class the mainwindow as a parent.