Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Keep previous window open when closing newest window?



  • I am new to QT and am working on a simple application for displaying information for certain data based on user input. To start, I have a login page (1st/main window), which if correct credentials are entered will open the next window, which I will call the 2nd window. When the user enters information to the 2nd window and presses the button, it opens and displays the data in charts. Call this the 3rd window. Following the input from the first window, I call "hide()" and then "show()" for the second window. When the button in the second window is pressed, I only call "show()" which opens the 3rd window. Ideally, the user can close the 3rd window and enter new data into the second window. However, closing the 3rd window also closes the 2nd window. Whats more weird is that I can click on the 2nd window, close it, and use the 3rd window without issue.

    So, why can I not close the 3rd window and keep the 2nd window open? And why can I close the 2nd window without closing the 3rd window (not that this is bad).


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you implementing each of these widgets in the preceding one ?



  • I believe I may have accidentally created the windows as “dialog without buttons”. Not sure if that would cause such an issue.

    But no, each of the windows has their own implementation. The header file of the windows has an object of the new window which it will open, and I use that to call NewWindowVarNamw->show()



  • @SGaist
    I was able to fix this by including the parent in the constructor vs creating the new window without the parent reference. So 2ndWindow = new 2ndWindow(text, this); works, but
    2ndWindow = new 2ndWindow(text); causes the weird behavior above.


  • Lifetime Qt Champion

    Passing the parent as parameter makes you new window a child of its parent. Without it, it's a top level widget.


  • Lifetime Qt Champion

    Hi
    Just as a note.
    The QApplication has a setting that will auto close the app if all top level windows are closed.
    https://doc.qt.io/qt-5/qguiapplication.html#quitOnLastWindowClosed-prop
    So i wondered if that what you saw when you closed 3rd window and window 2 also was closed.


Log in to reply