问题描述
我对KConfig的使用有疑问.我可以像这样在.kde4/share/config/_appname_rc配置文件中写入和读取设置
I've a question about KConfig usage. I'm able to write and read settings in my .kde4/share/config/_appname_rc configuration file like that
KConfig basicconf;
KConfigGroup conf = KConfigGroup(basicconf.group("Settings"));
conf.writeEntry("filepath",QString("/path/"));
basicconf.sync();
但是我不明白如何在首次运行我的应用程序时,或者万一需要重置应用程序设置时,如何使用默认"配置文件进行读取.
But I don't understand how to use a "default" configuration file to read at first time i run my application, or in case if application settings needs reset.
我宁愿不使用KConfig XT,因为我的项目很小,而且带有kcfgc *文件的KConfigXT似乎过多.
I prefer do not use KConfig XT because my project is tiny and KConfigXT with kcfgc* files seems excessive.
提前谢谢
推荐答案
首先,这是
KConfigGroup conf = KConfigGroup(basicconf.group("Settings"));
KConfigGroup conf = KConfigGroup(basicconf.group("Settings"));
可以写得更清楚,至少恕我直言,为:
can be written more clearly, at least imho, as:
KConfigGroup conf(& basicconf,设置");
KConfigGroup conf(&basicconf, "Settings");
还要注意,常规"是最常用的通用"组名.反正...
Also note that "General" is the most common "generic" group name used. Anyways...
您可以在应用程序中安装默认配置文件;将其安装到$ PREFIX/share/config/,可以很容易地在CMakeLists.txt文件中实现:
You can install a default config file with your application; install it to $PREFIX/share/config/, which is easily achieved with this in your CMakeLists.txt file:
安装(文件<您的配置文件>目标$ {CONFIG_INSTALL_DIR})
install(FILES <your config file> DESTINATION ${CONFIG_INSTALL_DIR})
KConfig处理了从那里进行合并的所有魔力;您不必做任何事情.
KConfig handles all the magic of merging from there; you don't have to do a thing.
至于KConfigXT被过度使用,使用它有很多好处,包括自动执行配置对话框,确保强制执行边界和合法值等.编写一个小文件,在CMakeLists.txt文件中弹出一个条目通常会很多比手工完成免费提供的工作要少.
As for KConfigXT being overkill, there are many benefits to using it, including automating your config dialogs, ensuring bounds and legal values are enforced, etc. Writing a small file, popping an entry in the CMakeLists.txt file is usually much less work than doing what it gives you for free by hand. There's a great tutorial on TechBase on this.
这篇关于如何在kdelib上使用KConfig加载默认设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!