KConfig Class

The central class of the KDE configuration data system. More...

Header: #include <KConfig>
CMake: find_package(KF6 REQUIRED COMPONENTS Config)
target_link_libraries(mytarget PRIVATE KF6::ConfigCore)
Inherits: KConfigBase
Inherited By:

KDesktopFile and KSharedConfig

Public Types

enum OpenFlag { IncludeGlobals, CascadeConfig, SimpleConfig, NoCascade, NoGlobals, FullConfig }
flags OpenFlags

Public Functions

KConfig(const QString &file = QString(), KConfig::OpenFlags mode = FullConfig, QStandardPaths::StandardLocation type = QStandardPaths::GenericConfigLocation)
KConfig(const QString &file, const QString &backend, QStandardPaths::StandardLocation type = QStandardPaths::GenericConfigLocation)
void addConfigSources(const QStringList &sources)
QStringList additionalConfigSources() const
void checkUpdate(const QString &id, const QString &updateFile)
KConfig *copyTo(const QString &file, KConfig *config = nullptr) const
QMap<QString, QString> entryMap(const QString &aGroup = QString()) const
bool isConfigWritable(bool warnUser)
QString locale() const
QStandardPaths::StandardLocation locationType() const
QString name() const
KConfig::OpenFlags openFlags() const
bool readDefaults() const
void reparseConfiguration()
bool setLocale(const QString &aLocale)
void setReadDefaults(bool b)

Static Public Members

QString mainConfigName()
void setMainConfigName(const QString &str)

Reimplemented Protected Functions

virtual void virtual_hook(int id, void *data) override

Detailed Description

Quickstart:

Get the default application config object via KSharedConfig::openConfig().

Load a specific configuration file:

KConfig config("/etc/kderc", KConfig::SimpleConfig);

Load the configuration of a specific component:

KConfig config("pluginrc");

In general it is recommended to use KSharedConfig instead of creating multiple instances of KConfig to avoid the overhead of separate objects or concerns about synchronizing writes to disk even if the configuration object is updated from multiple code paths. KSharedConfig provides a set of open methods as counterparts for the KConfig constructors.

See also KSharedConfig, KConfigGroup, <a, href="https://techbase.kde.org/index.php?title=Development/Tutorials/KConfig">the, techbase, HOWTO, on, and KConfig</a>.

Member Type Documentation

enum KConfig::OpenFlag
flags KConfig::OpenFlags

Determines how the system-wide and user's global settings will affect the reading of the configuration.

If CascadeConfig is selected, system-wide configuration sources are used to provide defaults for the settings accessed through this object, or possibly to override those settings in certain cases.

If IncludeGlobals is selected, the kdeglobals configuration is used as additional configuration sources to provide defaults. Additionally selecting CascadeConfig will result in the system-wide kdeglobals sources also getting included.

Note that the main configuration source overrides the cascaded sources, which override those provided to addConfigSources(), which override the global sources. The exception is that if a key or group is marked as being immutable, it will not be overridden.

Note that all values other than IncludeGlobals and CascadeConfig are convenience definitions for the basic mode. Do @em not combine them with anything. @see OpenFlags

The OpenFlags type is a typedef for QFlags<OpenFlag>. It stores an OR combination of OpenFlag values.

Member Function Documentation

[explicit] KConfig::KConfig(const QString &file = QString(), KConfig::OpenFlags mode = FullConfig, QStandardPaths::StandardLocation type = QStandardPaths::GenericConfigLocation)

Creates a KConfig object to manipulate a configuration file for the current application.

If an absolute path is specified for @p file, that file will be used as the store for the configuration settings. If a non-absolute path is provided, the file will be looked for in the standard directory specified by type. If no path is provided, a default configuration file will be used based on the name of the main application component.

@p mode determines whether the user or global settings will be allowed to influence the values returned by this object. See OpenFlags for more details.

@note You probably want to use KSharedConfig::openConfig instead.

@param file the name of the file. If an empty string is passed in and SimpleConfig is passed in for the OpenFlags, then an in-memory KConfig object is created which will not write out to file nor which requires any file in the filesystem at all. @param mode how global settings should affect the configuration options exposed by this KConfig object @param type The standard directory to look for the configuration file in

@sa KSharedConfig::openConfig(const QString&, OpenFlags, QStandardPaths::StandardLocation)

KConfig::KConfig(const QString &file, const QString &backend, QStandardPaths::StandardLocation type = QStandardPaths::GenericConfigLocation)

Creates a KConfig object using the specified backend. If the backend can not be found or loaded, then the standard configuration parser is used as a fallback.

@param file the file to be parsed @param backend the backend to load @param type where to look for the file if an absolute path is not provided

@since 4.1

@deprecated since 6.3, use other constructor

void KConfig::addConfigSources(const QStringList &sources)

Adds the list of configuration sources to the merge stack.

Currently only files are accepted as configuration sources.

The first entry in @p sources is treated as the most general and will be overridden by the second entry. The settings in the final entry in @p sources will override all the other sources provided in the list.

The settings in @p sources will also be overridden by the sources provided by any previous calls to addConfigSources().

The settings in the global configuration sources will be overridden by the sources provided to this method (@see IncludeGlobals). All the sources provided to any call to this method will be overridden by any files that cascade from the source provided to the constructor (@see CascadeConfig), which will in turn be overridden by the source provided to the constructor.

Note that only the most specific file, ie: the file provided to the constructor, will be written to by this object.

The state is automatically updated by this method, so there is no need to call reparseConfiguration().

@param sources A list of extra config sources.

QStringList KConfig::additionalConfigSources() const

Returns a list of the additional configuration sources used in this object

void KConfig::checkUpdate(const QString &id, const QString &updateFile)

Ensures that the configuration file contains a certain update.

If the configuration file does not contain the update @p id as contained in @p updateFile, kconf_update is run to update the configuration file.

If you install config update files with critical fixes you may wish to use this method to verify that a critical update has indeed been performed to catch the case where a user restores an old config file from backup that has not been updated yet.

@param id the update to check @param updateFile the file containing the update

KConfig *KConfig::copyTo(const QString &file, KConfig *config = nullptr) const

Copies all entries from this config object to a new config object that will save itself to @p file.

The configuration will not actually be saved to @p file until the returned object is destroyed, or sync() is called on it.

Do not forget to delete the returned KConfig object if @p config was 0.

@param file the new config object will save itself to @param config if not 0, copy to the given KConfig object rather than creating a new one

@return @p config if it was set, otherwise a new KConfig object

QMap<QString, QString> KConfig::entryMap(const QString &aGroup = QString()) const

Returns a map (tree) of entries in a particular group.

The entries are all returned as strings.

@param aGroup The group to get entries from.

@return A map of entries in the group specified, indexed by key. The returned map may be empty if the group is empty, or not found. @see QMap

bool KConfig::isConfigWritable(bool warnUser)

Whether the configuration can be written to.

If @p warnUser is true and the configuration cannot be written to (ie: this method returns @c false), a warning message box will be shown to the user telling them to contact their system administrator to get the problem fixed.

The most likely cause for this method returning @c false is that the user does not have write permission for the configuration file.

@param warnUser whether to show a warning message to the user if the configuration cannot be written to

@returns true if the configuration can be written to, false if the configuration cannot be written to

QString KConfig::locale() const

Returns the current locale.

See also setLocale().

QStandardPaths::StandardLocation KConfig::locationType() const

Returns the standard location enum passed to the constructor. Used by KSharedConfig. @since 5.0

[static] QString KConfig::mainConfigName()

Get the name of application config file. @since 5.93

See also setMainConfigName().

QString KConfig::name() const

Returns the filename used to store the configuration.

KConfig::OpenFlags KConfig::openFlags() const

@return the flags this object was opened with @since 5.3

bool KConfig::readDefaults() const

@returns @c true if the system-wide defaults will be read instead of the user's settings

See also setReadDefaults().

void KConfig::reparseConfiguration()

Updates the state of this object to match the persistent storage. Note that if this object has pending changes, this method will call sync() first so as not to lose those changes.

bool KConfig::setLocale(const QString &aLocale)

Sets the locale to @p aLocale.

The global locale is used by default.

@note If set to the empty string, @b no locale will be matched. This effectively disables reading translated entries.

@return @c true if locale was changed, @c false if the call had no effect (eg: @p aLocale was already the current locale for this object)

See also locale().

[static] void KConfig::setMainConfigName(const QString &str)

Sets the name of the application config file. @since 5.0

See also mainConfigName().

void KConfig::setReadDefaults(bool b)

When set, all readEntry calls return the system-wide (default) values instead of the user's settings.

This is off by default.

@param b whether to read the system-wide defaults instead of the user's settings

See also readDefaults().

[override virtual protected] void KConfig::virtual_hook(int id, void *data)

Reimplements: KConfigBase::virtual_hook(int id, void *data).

Virtual hook, used to add new "virtual" functions while maintaining binary compatibility. Unused in this class.