[Development] Qt Configuration Generation from Schema

Tomaz Canabrava tcanabrava at kde.org
Sun Jan 1 10:35:23 CET 2017


A *long* time ago I started trying to create a replacement for QSettings in
json, but if failed for the following reasons:

- Different Views on what it should accomplish - I envisioned the JSON as a
meta-language for generating the C++ code for the QSettings, while others
tougth I was using JSON as the settings definitions on disk

- Json has no notion of int / floating point, everything is floating point,
and so on the json file I had to add an entry for the type of the setting
for every setting, making that option quite verbose.

- Json is not the default configuration file for windows / mac / android /
ios, while usual unix has no default, that's a stopper per see, as some
projects should use the default on each system.

So I tried a different approach this time, creating a qml-like sintax for
the configuration file, a pre-processor that generates C++ code for
QSettings and it's easily used with Widgets / QML code,

It looks a bit like KConfigXT without the extras and written in something
less verbose than XML, and imo my approach  also generates a better c++
class chain than KConfigXT (on KConfigXT all the preferences members are
"global" from within the main Preferences class, while on my they are
agreggated in subclasses).

It also provides a signal / slot for each setting, so a change on any
setting will change the QML component that interacts with it:

preferences.conf file:

#include <QColor>

Preferences {
     Window {
         QColor color  = "#FF0000"

QML usage:

Rectangle {
     backgroundColor : preferences.window.color

onSomething {
    preferences.window.color = "black"

C++ usage:

#include "genreated_preferences.h"


connect(Preferences::window(), &Window::colorChanged, [=](const QColor& c)
{ setBackgroundColor(c});

Preferences::self()->window()->setColor( QColor("#010101") );


the current code works (as in generates the correct c++ classes), there's
plenty of unittests with examples on how to write the configuration files,
and I tested all the compiled files against a simple application to be sure
there are no obvious bugs.

This was my first try at a C++ project that didn't used Qt so if there's
anything wrong there I'll fix, and please don't hit the developer :)

code currently is in https://github.com/tcanabrava/configuration-parser

If this is something that qt has interest on integrating I'll gladly change
everything on the project necessarely for that.

Tomaz Canabrava
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20170101/5e40a570/attachment.html>

More information about the Development mailing list