How to load default settings with KConfig on kdelib?

对着背影说爱祢 提交于 2019-12-04 21:08:25

First, this:

KConfigGroup conf = KConfigGroup(basicconf.group("Settings"));

can be written more clearly, at least imho, as:

KConfigGroup conf(&basicconf, "Settings");

Also note that "General" is the most common "generic" group name used. Anyways...

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:

install(FILES <your config file> DESTINATION ${CONFIG_INSTALL_DIR})

KConfig handles all the magic of merging from there; you don't have to do a thing.

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.

Use KGlobal::config() to get a pointer to the default KConfig object owned by your app. It automagically refers to the file in $KDEHOME/share/config.

KConfig XT can make sense because

  • the API of the generated YourSettings object is specific to your application; it's a bit easier to understand the meaning of YourSettings::setFilePath(path) than conf.writeEntry("filepath", path);
  • Your app may grow; it's easier to start with KConfig XT than rip and replace it later.
  • Defaults are specified in the via the .kcfg XML rather than hardcoded in a few places of your app, and you have a setDefault() method to reset all values.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!