Search code examples
qtqwidget

Closing new nonchild window in Qt


i'm trying to make 2 windows. 2nd should be called in 1st. I don't connect them child->parent. But when i called 2nd window and closed it 1st window closed too. What should i do? Both windows are inhereted from QWidget. C++ and Qt


Sorry for my poor describe. I have a main window. Class inherited from QMainWindow. That i created 2nd window. Class inherited from QWidget. In first (main window) i'm creating and calling 2nd window

ConfigWindow *ConfWindow = new ConfigWindow();
ConfWindow->show();

Without giving link to parent. Everything works fine, but when i close 2nd window (config-window) my main window is closing too. I needn't in this. What should i do to block closing main window after config-window closing.

Hope describe a little better.

My first window has this flags:

this->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);

Without them everything is fine. Could i change something if i need that flags in my window?


Solution

  • You need something like:

    QApplication app(argc, argv);
    app.setQuitOnLastWindowClosed(false);
    

    Here is the test program: http://pastebin.com/f5903c5f4.

    Beware, now you need to explicitly call quit() in the destructor of your main window.

    If you read QApplication::quitOnLastWindowClosed documentation, you will find out that:

    If this property is true, the applications quits when the last visible primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose attribute set is closed. By default this attribute is set for all widgets except for sub-windows

    Because your main window is a (frameless) tool window, it does count. That leaves ConfWindow as the only non sub-windows top-level widget. Thus, if you close ConfWindow, it provokes the application instance to quit.