Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

KDE Frameworks - Part 1

KConfig, for Storing and Fetching Configuration Data

Syntax highlighting in Kate is powered by the Syntax Highlighting Framework

find_package(KF5Config)

...

target_link_libraries(myapp PRIVATE KF5::ConfigCore)
QT += KConfigCore
#include <KConfig>
#include <KConfigGroup>
#include <QDebug>

int main() {

    KConfig config("myappsettings");

    KConfigGroup general = config.group("General");

    qDebug() << general.readEntry("someSetting", "A default value");

    general.writeEntry("someSetting", "A new value");

    qDebug() << general.readEntry("someSetting", "A default value");

}
<?xml version="1.0" encoding="UTF-8"?>
<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
      http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" >
  <kcfgfile name="myappsettings" />
  <group name="General">
    <entry name="someSetting" type="String">
      <label>A setting</label>
      <default>A default value</default>
    </entry>
  </group>
</kcfg>
File=myappsettings.kcfg
ClassName=MyAppSettings
Mutators=true
DefaultValueGetters=true
GenerateProperties=true
#include "myappsettings.h"
#include <QDebug>

int main() {

    MyAppSettings settings;

    qDebug() << settings.someSetting();

    settings.setSomeSetting("A new value");

    qDebug() << settings.someSetting();
}
qmlRegisterSingletonInstance<MyAppSettings>("myapp", 1, 0, "Settings", &settings);
import myapp 1.0

console.log(Settings.someSetting)
kconfig_add_kcfg_files(myapp myappsettings.kcfgc)

About KDAB


4 Comments

26 - Aug - 2021

Till Schäfer

26 - Aug - 2021

Nicolas Fella

26 - Aug - 2021

Till Schäfer

27 - Aug - 2021

Daniele Mte90 Scasciafratte

NicolasFella

Nicolas Fella

Software Engineer