在Qt中安全地退出应用程序并关闭系统
在Linux+Qt环境中,直接使用system("poweroff")调用进行重新启动可能导致程序来不及安全退出,此时,可以考虑结合poweroff或reboot指令的-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())); }
页面版本: 2, 最后编辑于: 26 Apr 2023 06:41