当我要退出应用程序时,如何在QT中自动调用destructor

How automatically call destructor in qt when I want to exit the application?

本文关键字:QT destructor 调用 退出 我要 应用程序      更新时间:2023-10-16

i使用CAMA系列(手指打印模块)作为我的应用程序。在该文档中说,建议在退出应用程序之前使用EnrollStandByMode命令。我创建此命令,并在应用程序中的qmainwindow上的destructor中称其为。(这里没有问题)。但是,当我退出应用程序并再次运行时,我的手指打印会混淆,并且响应命令不正确。跟踪我的代码后,我看到destructor在应用程序关闭时未运行。

这是我退出QAPP的代码。

void MainWindow::on_pushButton_clicked()
{
    qDebug()«"QApplication::quit()";
    QApplication::quit();
}  

我想知道如何在退出应用程序之前致电Destructor?

在应用程序末尾有几种调用某些东西的方法。

如果您的代码不需要QT事件循环运行,只需在main()

的末尾调用该功能
int main(int argc, char **argv)
{
    QApplication app(argc, argv); // or QGuiApplication/QCoreApplication
    // application setup
    const int resultCode = app.exec();
    // call your shutdown code here
    return resultCode;
}

另一个选项是用qAddPostRoutine()注册函数,该功能由QT应用程序对象的destructor调用。

如果您需要QT事件循环,请将功能连接到QT应用程序对象的aboutToQuit()信号。