2.2 KiB
title | date | draft | tags | ||||
---|---|---|---|---|---|---|---|
Getting a clean config directory, with KConfigXT! | 2023-10-08 | false |
|
This is a sort-of reply to Herzenschein's blog post from a few months ago. He goes over how to tell KConfig put it's files into app-specific folders instead of dumping them into the garbage bin of ~/.config
. He noted that he hasn't touched KConfigXT yet, so this is how to make it work with KConfigXT applications.
However you'll notice by default KConfigXT generates constructors for your configuration class like this:
class Config : public KConfigSkeleton
{
Q_OBJECT
public:
Config( QObject *parent );
~Config() override;
...
That's not useful, and it took me a minute to figure out how to allow KConfigSkeleton and the KConfig compiler to let me pass AppConfigLocation
. However, the solution is very simple.
First, you need to make sure it's not a Singleton
. (There's probably a way to make it work with a singleton though.) Make sure that setting is turned off in your .kcfgc
:
File=config.kcfg
ClassName=Config
Mutators=true
DefaultValueGetters=true
GenerateProperties=true
Singleton=false
And then in your .kcfg
, modify the <kcfgfile>
block to add arg = "true"
instead of hardcoding a filename. It will look something like this:
<kcfgfile arg="true" />
Once you run the compiler again, you'll get a constructor that allows you to pass a KSharedConfig!
class Config : public KConfigSkeleton
{
Q_OBJECT
public:
Config( KSharedConfig::Ptr config = KSharedConfig::openConfig() );
~Config() override;
...
And then you call the usual function:
new Config(KSharedConfig::openConfig("myappconfigrc", KConfig::SimpleConfig, QStandardPaths::AppConfigLocation));
Now enjoy your configuration file living in it's own directory! Here's the constructor parameter logic in KConfig which was essential in figuring this out.