欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 八卦 > Qt在Win,Mac和Linux的开机自启设置

Qt在Win,Mac和Linux的开机自启设置

2025/2/13 14:03:48 来源:https://blog.csdn.net/H520xcodenodev/article/details/142879137  浏览:    关键词:Qt在Win,Mac和Linux的开机自启设置

Windows

Windows 使用注册表来管理开机自启的应用程序。

void runWithSystem(const QString& name, const QString& path, bool autoRun) {QSetting reg("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSetting::NativeFormat);reg.setValue(name, autoRun ? path : "");
}

Linux

Linux 使用 ~/.config/autostart/*.desktop 来管理开机自启的应用程序。

void runWithSystem(const QString& name, const QString& path, bool autoRun) {QString desktopFilePath = QStandarPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/autostart" + name + ".desktop";if (autoRun) {QFile file(desktopFilePath);if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {QTextStream out(&file);out << "[Desktop Entry]\n"<< "Type=Application\n"<< "Name=" << name << "\n"<< "Exec=" << path << "\n"<< "X-GNOME-Autostart-enabled=true";file.close();}}else {QFile::remove(desktopFilePath);}
}

Macos

Macos 采用 launchd 来管理启动项。

void runWithSystem(const QString& name, const QString& path, bool autoRun) {QString plistPath = QStandardPaths::writableLocation(QStandards::HomeLocation) + "/Library/LaunchAgents/" + name + ".plist";if (autoRun) {QFile file(plistPath);if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {QTextStream out(&file);out << "<?xml version\"1.0\" encoding=\"UTF-8\"?>\n""!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\"\n""	\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n""<plist version=\"1.0\">\n""<dict>\n""	<key>Label</key>\n""	<string>" << name << "</string>\n""	<key>ProgramArguments</key>\n""	<array>\n""		<string>" << path << "</string>\n""	</array>\n""	<key>RunAtLoad</key>\n""	<true/>\n""</dict>\n""</plist>";file.close();}}else {QFile::remove(plistPatch);}
}

资料参考:https://github.com/0voice

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com