退出 Qt 程序的正确方法?

发布于 2024-12-13 22:44:04 字数 250 浏览 7 评论 0原文

我应该如何退出 Qt 程序,例如加载数据文件时,发现文件损坏,并且用户需要退出该应用程序或重新启动数据文件?

我应该:

  1. 调用 exit(EXIT_FAILURE)
  2. 调用 QApplication::quit()
  3. 调用 QCoreApplication::quit()

以及(2)和之间的区别(3)?

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:

  1. call exit(EXIT_FAILURE)
  2. call QApplication::quit()
  3. call QCoreApplication::quit()

And difference between (2) and (3)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

喜爱皱眉﹌ 2024-12-20 22:44:04

QApplication 派生自 QCoreApplication,因此继承了 quit(),它是 QCoreApplication 的公共槽,因此与 QApplication::quit() 没有区别。代码>和QCoreApplication::quit()

正如我们可以在 QCoreApplication::quit()“告诉应用程序退出并返回代码 0(成功)。”。如果您因为发现文件损坏而想要退出,那么您可能不希望以返回代码零退出(这意味着成功),因此您应该调用 QCoreApplication::exit() 因为您可以提供一个非零 returnCode,按照惯例,它指示错误。

重要的是要注意“如果事件循环未运行,则此函数 (QCoreApplication::exit()) 不执行任何操作”,因此在这种情况下,您应该调用 exit(EXIT_FAILURE) 。

QApplication is derived from QCoreApplication and thereby inherits quit() which is a public slot of QCoreApplication, so there is no difference between QApplication::quit() and QCoreApplication::quit().

As we can read in the documentation of QCoreApplication::quit() it "tells the application to exit with return code 0 (success).". If you want to exit because you discovered file corruption then you may not want to exit with return code zero which means success, so you should call QCoreApplication::exit() because you can provide a non-zero returnCode which, by convention, indicates an error.

It is important to note that "if the event loop is not running, this function (QCoreApplication::exit()) does nothing", so in that case you should call exit(EXIT_FAILURE).

你的呼吸 2024-12-20 22:44:04

您可以调用qApp->exit();。我一直用它并且从来没有遇到过问题。

如果您的应用程序是命令行应用程序,您可能确实希望返回退出代码。代码是什么完全取决于您。

You can call qApp->exit();. I always use that and never had a problem with it.

If you application is a command line application, you might indeed want to return an exit code. It's completely up to you what the code is.

撩起发的微风 2024-12-20 22:44:04

在搜索这个问题时,我在 文档 中发现了这个示例。

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

当然,比照您的特定操作。

连同这张纸条。

最好始终使用
排队连接。如果连接(非排队)到该插槽的信号是
在控制进入主事件循环之前发出(例如在“int
main" 调用 exec()),槽没有任何作用,应用程序永远不会
退出。使用排队连接可确保插槽不会被
直到控制进入主事件循环后调用。

连接 QGuiApplication::lastWindowClosed() 信号是常见的
退出()

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()

開玄 2024-12-20 22:44:04

如果您使用 Qt Jambi,这应该可以工作:

QApplication.closeAllWindows();

If you're using Qt Jambi, this should work:

QApplication.closeAllWindows();
人海汹涌 2024-12-20 22:44:04

如果您需要从 main() 关闭您的应用程序,您可以使用此代码

int main(int argc, char *argv[]){
QApplication app(argc, argv);
...
if(!QSslSocket::supportsSsl()) return app.exit(0);
...
return app.exec();
}

如果未安装 OpenSSL,程序将终止

if you need to close your application from main() you can use this code

int main(int argc, char *argv[]){
QApplication app(argc, argv);
...
if(!QSslSocket::supportsSsl()) return app.exit(0);
...
return app.exec();
}

The program will terminated if OpenSSL is not installed

绳情 2024-12-20 22:44:04
//How to Run App

bool ok = QProcess::startDetached("C:\\TTEC\\CozxyLogger\\CozxyLogger.exe");
qDebug() <<  "Run = " << ok;


//How to Kill App

system("taskkill /im CozxyLogger.exe /f");
qDebug() << "Close";

示例

//How to Run App

bool ok = QProcess::startDetached("C:\\TTEC\\CozxyLogger\\CozxyLogger.exe");
qDebug() <<  "Run = " << ok;


//How to Kill App

system("taskkill /im CozxyLogger.exe /f");
qDebug() << "Close";

example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文