保存配置在qt linux [英] saving configs in qt linux

查看:162
本文介绍了保存配置在qt linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为linux写了一个qt应用程序。应用程序应该在启动时运行 - i
使用桌面条目。

i wrote a qt application for linux. The application is supposed to run at startup- which i did with a desktop entry.

但我需要它更复杂:

but i need it to be more complicated: there is a checkbox that the user supposed to check in order to choose whether the application will run at startup or not.

我想如何保存他的prefference?

how do i suppose to save his prefference?

应用程序是windows的wriiten,然后保存在注册表中。
我从谷歌搜索,我应该保存在/ etc。

the application was wriiten for windows before and this was saved in the registry. i got from googling that i should save it in /etc.

什么文件应该是?我怎么把它写在我的代码?
,我可以添加一个条件到桌面项,或者我应该运行一些脚本?

what file should it be? how do i write it in my code? and could i add a condition to the desktop entry, or should i run some script?

我在这一切都很新,所以我会详细答案。

i'm quite new in all this, so i will appriciate a detailed answer.

感谢u。

推荐答案

case,保存控制app是否应该在启动时运行的首选项设置是完全没有意义的。自动运行条目桌面文件的存在反映了该首选项的状态。如果该文件存在,请选中复选框。如果用户取消选中该复选框,您将删除该文件。如果用户选中复选框,则创建文件。而已。复制偏好存储中的设置只会导致错误,因为现在您必须保持文件系统中的文件的设置和存在同步,并且您必须处理各种各样的角落情况。

For this particular case, saving a preference setting controlling whether the app should run at startup or not is completely pointless. The very existence of the autorun entry desktop file reflects the state of that preference. If that file exists, you check the checkbox. If the user unchecks the checkbox, you delete the file. If the user checks the checkbox, you create the file. That's it. Duplicating the setting in a preference store will only lead to bugs since now you have to keep the setting and the presence of the file in the file system in sync and you have to handle all sorts of corner cases.

此外,请记住, / etc / xdg / autostart 是系统范围的自动运行条目。如果它应该是每个用户的设置,您应该在用户的autostart目录中创建.desktop文件。要确定其位置,请按照桌面应用程序自动启动规范,它要求位置为 $ XDG_CONFIG_DIRS / autostart ,通常解析为用户的 .config / autostart 目录home(但是,如果 XDG_CONFIG_DIRS 环境变量存在,您应该先读取该值,然后附加 / autostart

Additionally, please keep in mind that /etc/xdg/autostart is for system-wide autorun entries. If it's supposed to be a per-user setting, you should create the .desktop file in the user's autostart directory. To determine its location, please follow the Desktop Application Autostart Specification, which mandates that the location be $XDG_CONFIG_DIRS/autostart, which typically resolves to the .config/autostart directory in the user's home (however, if the XDG_CONFIG_DIRS environment variable exists, you should resolve it by reading that value first then appending /autostart to it.)

这是一个示例程序,将打印出您想要的内容:

Here is an example program that will print out what you want:

#include <cstdlib>
#include <iostream>
#include <QtCore/QString>
#include <QtCore/QDir>

#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;
}

这篇关于保存配置在qt linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆