在Qt中安全地退出应用程序并关闭系统

在Linux+Qt环境中,直接使用system("poweroff")调用进行重新启动可能导致程序来不及安全退出,此时,可以考虑结合poweroffreboot指令的-d参数,与Qt的事件特性,使用下面的方式:

void Shutdown() {
    //Request the OS to shutdown after 1 sec, executes the command asynchronously
    system("poweroff -d 1 &");
    //Exit the app safely
    QTimer::singleShot(245, qApp, SLOT(quit()));
}
 
void Reboot() {
    //Request the OS to reboot after 1 sec, executes the command asynchronously
    system("reboot -d 1 &");
    //Exit the app safely
    QTimer::singleShot(245, qApp, SLOT(quit()));
}
it
除非特别注明,本页内容采用以下授权方式: Creative Commons Attribution-ShareAlike 3.0 License