Why does a program developed with Qt not exit when one main window is closed, but waits until all windows are closed before exiting?

This behavior is caused by the event loop (Event Loop) mechanism in the Qt framework. When a Qt application is executed, it will enter an event loop, which is responsible for processing user input, events and signals, and calling the corresponding slot functions or handlers accordingly.

When you close the main window of a Qt application, the main window emits a close event. By default, Qt will pass this shutdown event to the application's event loop. The event loop checks to see if there are any other windows open for the current application. If there are other windows open, the event loop continues running, waiting for other events to be processed. The event loop ends only when all windows are closed, causing the application to exit.

This design has several advantages:

  1. In a multi-window application, the user can close the main window and continue working on other sub-windows without accidentally exiting the entire application.
  2. Allows the application to handle shutdown events, such as performing some cleanup operations or asking the user whether to save unsaved data.
    You can use the QCoreApplication::quit() or QApplication::quit() method to manually terminate the event loop and exit the application.
  3. If you want to quit the application immediately when closing the main window without having to wait for other windows to close, you can use the Qt::WA_QuitOnClose property. Setting this property to true tells Qt to exit the application immediately when the main window is closed. For example, you can add the following code in the constructor of the main window class:
setWindowFlag(Qt::WA_QuitOnClose, true);

After setting this, when the main window is closed, the application will exit immediately without waiting for other windows to close.

It should be noted that this property is only valid for the main window and has no effect on other non-main window windows.

QCoreApplication::aboutToQuit() is a signal provided by Qt that is emitted just before the application is about to exit. You can perform some cleanup operations or other necessary operations by connecting this signal to the corresponding slot function.

Here is a simple example using the QCoreApplication::aboutToQuit() signal:

#include <QCoreApplication>
#include <QDebug>

class MyApplication : public QCoreApplication
{
    
    
public:
    MyApplication(int& argc, char** argv) : QCoreApplication(argc, argv)
    {
    
    
        // 连接 aboutToQuit 信号到槽函数
        connect(this, &QCoreApplication::aboutToQuit, this, &MyApplication::cleanup);
    }

private slots:
    void cleanup()
    {
    
    
        // 在应用程序即将退出时执行清理操作
        qDebug() << "Performing cleanup before application quits...";
        // 在这里可以添加你的文件保存操作或其他清理逻辑

        // 退出应用程序
        quit();
    }
};

int main(int argc, char** argv)
{
    
    
    MyApplication app(argc, argv);

    // 在这里添加你的应用程序逻辑
    // ...

    // 执行应用程序事件循环
    return app.exec();
}

In the above example, we created a custom MyApplication class that inherits from QCoreApplication. In the constructor, we use the connect() function to connect the aboutToQuit signal to the cleanup() slot function. In the cleanup() slot function, you can perform any cleaning operations you need to perform before the application exits, such as saving files or closing network connections. Finally, we call the quit() method in the cleanup() function to exit the application.

Note that if you are using QApplication instead of QCoreApplication, you can inherit the MyApplication class from QApplication and adjust accordingly.

Guess you like

Origin blog.csdn.net/zhangzhechun/article/details/132147493