:: printable version ::

XML application preferences handler

Description

The XMLPreferences class allows to store application settings in plain text files using XML format. Stored settings may be read using the XMLPreferences class or other XML parsers. The class supports many Qt data types and allows settings to be organized in sections.

The following data types are currently supported. Custom data types can be easily added (please refer to the comments in the .cpp file).

The following data types are only available if the QT_CORE_ONLY macro has not been defined (you might need this if you do not link against the QtGui module):

The class requires both the XmlWriter and Base64 classes from The OSDaB Project and the libxml2 library.

A small example application can be downloaded below. The package contains all the required source files, the libxml2 headers and the the Win32 libraries build and distributed by Igor Zlatkovic. Unix users should have a working libxml2-devel installation (I'd wonder if they don't have one already!).

Changelog

  • 2007-01-28 Major changes:
    • The class is a singleton now
    • Better error handling and notification
    • Product version and info handling
    • Massive internal refactoring
    • Some API changes (sorry, but I hope the new ones are better ;)
    • Better handling of the default section
    • Lots of performance issues solved
  • 2006-06-18 Bug fix: updated calls to Base64 class to reflect the latest Base64 API (thanks to Alexander Saal).
  • 2006-06-09 Minor improvements and API changes; updated to use the latest XMLWriter.
  • 2006-01-08 First public release.

Requirements

  • Qt 4.0.x (QtCore and optional QtGui module)
  • libxml2 library
  • OSDaB XmlWriter (latest version please!)
  • OSDaB Base64 (latest version please!)

Features

  • Store settings in plain text files using XML format.
  • Supports many Qt data types.
  • Supports encoding of binary data as plain text.
  • May be compiled without linking the QtGui library.
::top::

Usage example

Here is a little usage example for storing settings to a file:

XMLPreferences xml("MyApplication", "MyCompany"); xml.setInt("world_takeover", "days_missing", 5); xml.setByteArray("appearance", "window_state", saveState()); xml.setRect("appearance", "geometry", frameGeometry());

And here is the code to load the previously stored settings:

XMLPreferences xml("MyApplication", "MyCompany"); if (!xml.load(mySettingsPath)) // ERROR HANDLING?
bool ok; int timeout = xml.getInt("world_takeover", "days_missing", &ok); QByteArray b = xml.getByteArray("appearance", "window_state"); if (!b.isEmpty()) restoreState(b); QRect r = xml.getRect("appearance", "geometry"); if (r.isValid()) setGeometry(r);
:: index :: top ::