对于这种特殊情况,保存控制应用程序是否应该在启动时运行的首选项设置是完全没有意义的.自动运行条目桌面文件的存在反映了该首选项的状态.如果该文件存在,请选中该复选框.如果用户取消选中该复选框,则删除该文件.如果用户选中该复选框,则创建该文件.而已.在首选项存储中复制设置只会导致错误,因为现在您必须保持文件系统中文件的设置和存在同步,并且您必须处理各种极端情况.
此外,请记住/ etc / xdg / autostart用于系统范围的自动运行条目.如果它应该是每用户设置,则应在用户的自动启动目录中创建.desktop文件.要确定其位置,请遵循Desktop Application Autostart Specification,其中要求该位置为$XDG_CONFIG_DIRS / autostart,通常解析为用户家中的.config / autostart目录(但是,如果存在XDG_CONFIG_DIRS环境变量,则应通过首先读取该值然后附加/自动启动它.)
这是一个打印出你想要的程序的示例程序:
#include
#include
#include
#include
#ifndef Q_OS_UNIX
#error This method only makes sense on Unix,use OS-specific handling for other OSes.
#endif
QString getUserXdgConfigDir()
{
QString result(std::getenv("XDG_CONFIG_DIRS"));
if (result.isEmpty()) {
// XDG_CONFIG_DIRS is not defined,we'll use the default value
// as mandated by http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html
result = QDir::homePath() + QDir::separator() + ".config";
}
return result;
}
QString getUserAutostartDir()
{
return getUserXdgConfigDir() + QDir::separator() + "autostart";
}
int main(int argc,char *argv[])
{
std::cout << "User config dir is " << getUserXdgConfigDir().toStdString() << std::endl;
std::cout << "User autostart dir is " << getUserAutostartDir().toStdString() << std::endl;
return 0;
}