Correct way to quit a Qt program?

前端 未结 6 1518
半阙折子戏
半阙折子戏 2020-12-04 08:07

How should I quit a Qt Program, e.g when loading a data file, and discovered file corruption, and user need to quit this app or re-initiate data file?

Should I:

6条回答
  •  旧巷少年郎
    2020-12-04 08:44

    While searching this very question I discovered this example in the documentation.

    QPushButton *quitButton = new QPushButton("Quit");
    connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection);
    

    Mutatis mutandis for your particular action of course.

    Along with this note.

    It's good practice to always connect signals to this slot using a QueuedConnection. If a signal connected (non-queued) to this slot is emitted before control enters the main event loop (such as before "int main" calls exec()), the slot has no effect and the application never exits. Using a queued connection ensures that the slot will not be invoked until after control enters the main event loop.

    It's common to connect the QGuiApplication::lastWindowClosed() signal to quit()

提交回复
热议问题