Important: Please read the Qt Code of Conduct -

MainWindow closed when child Widget Close invoked

  • Hello everybody,

    In my application I call a child QWidget to do some treatement and when finishing and closing it I want to hide the mainwindow. The problem is that when call closeEvent() from the child Widget, it close the application (the mainwindow).

    Code in the MainWindow:

    frmWindow::frmWindow(QWidget *parent) : QMainWindow(parent) // constructor
    connect(frOrdo, SIGNAL(minimiseTout()), this, SLOT(MinimiserTout())); // frOrdo is the child QWidget et MinimiseTout is a slot I invoke it
    void frmWindow::minimiserTout()
    	// Cache la fenêtre (et appelle closeEvent()).
    	// Enlève l'indication que la fenêtre est minimisée
        setWindowState(windowState() & ~Qt::WindowMinimized);

    and in the child QWidget, I've this :

    void frmOrdo::closeEvent(QCloseEvent *event)
    		emit changeOrdo();
    		emit closeWindow();
    		emit minimiseTout();	 

    the signal minimiseTout will be intercepted in the mainWindow and call the MinimiserTout function witch must hide the MainWindow but it's closed.

    Can anyone tell me where I'm wrong and how to proceed to resolve the problem

  • Moderators


    1. hiding and minimizing doesn't work. Just use one of the 2 methods, not both. And i guess this is what confuses you. hide() makes the window "disappear" from the screen.
    setWindowState(windowState() & ~Qt::WindowMinimized);
    1. what does emit closeWindow(); do just before the minimiseTout() signal emission?

  • Thansk for your response.

    I've commented the

    setWindowState(windowState() & ~Qt::WindowMinimized);

    and closeWindow(); signal. In fact I've added to call a function witch will close the chid Widget. It's not used now.

    But calling hide only in the minimiserTout() function still closes the mainwindow.

    I don't know if it must do it ?

  • Add a signal in widget child. Instantiate an object for widget child as a member data in the mainwindow class. Now, connect the signal of the widget child inside the mainwindow with the hide() function which should belong to the mainwindow(). Now you can emit the signal any time.

  • Lifetime Qt Champion


    How are you using the frmOrdo class ?

Log in to reply