# class KPrefs | Class for handling preferences settings for an application. More...
Definition | `#include <klineakconfig/kprefs.h>`
Inherited by | KLPrefs
List of all Methods
Annotated List
Files
Globals
Hierarchy
Index

#### Public Methods

- KPrefs (const QString &configname=QString::null)
- virtual ~KPrefs ()
- void setDefaults ()
- void readConfig ()
- void writeConfig ()
- void addItem (KPrefsItem *)
- void addItemBool (const QString &key,bool *reference, bool defaultValue=false)
- void addItemInt (const QString &key,int *reference, int defaultValue=0)
- void addItemColor (const QString &key,QColor *reference, const QColor &defaultValue=QColor(128,128,128))
- void addItemFont (const QString &key,QFont *reference, const QFont &defaultValue=QFont("helvetica",12))
- void addItemString (const QString &key,QString *reference, const QString &defaultValue="")
- void addItemStringList (const QString &key,QStringList *reference, const QStringList &defaultValue=QStringList())
- void addItemIntList (const QString &key,QValueList *reference, const QValueList &defaultValue=QValueList())

#### Public Static Methods

- static void setCurrentGroup (const QString &group)

#### Protected Methods

- virtual void usrSetDefaults ()
- virtual void usrReadConfig ()
- virtual void usrWriteConfig ()
- KConfig * config () const

#### Private Members

- static QString *mCurrentGroup
- KConfig *mConfig
- QPtrList mItems

## Detailed Description

This class provides an interface to preferences settings. Preferences items can be registered by the addItem() function corresponding to the data type of the seetting. KPrefs then handles reading and writing of config files and setting of default values.

Normally you will subclass KPrefs, add data members for the preferences settings and register the members in the constructor of the subclass.

Example:

      class MyPrefs : public KPrefs {
        public:
          MyPrefs()
          {
            setCurrentGroup("MyGroup");
            addItemBool("MySetting1",&mMyBool,false);
            addItemColor("MySetting2",&mMyColor,QColor(1,2,3));

            setCurrentGroup("MyOtherGroup");
            addItemFont("MySetting3",&mMyFont,QFont("helvetica",12));
          }

          bool mMyBool;
          QColor mMyColor;
          QFont mMyFont;
      }

It might be convenient in many cases to make this subclass of KPrefs a singleton for global access from all over the application without passing references to the KPrefs object around.

You can set all values to default values by calling setDefaults(), write the data to the configuration file by calling writeConfig() and read the data from the configuration file by calling readConfig().

If you have items, which are not covered by the existing addItem() functions you can add customized code for reading, writing and default setting by implementing the functions usrSetDefaults(), usrReadConfig() and usrWriteConfig().

Internally preferences settings are stored in instances of subclasses of KPrefsItem. You can also add KPrefsItem subclasses for your own types and call the generic addItem() to register them.

See also: KPrefsItem

KPrefs (const QString &configname=QString::null) | ### KPrefs

Constructor.

Parameters:

configname | name of config file. If no name is given, the default config file as returned by kapp()->config() is used.

~KPrefs () | ### ~KPrefs

[virtual]

Destructor

void setDefaults () | ### setDefaults

Set preferences to default values. All registered items are set to their default values.

void readConfig () | ### readConfig

Read preferences from config file. All registered items are set to the values read from disk.

void writeConfig () | ### writeConfig

Write preferences to config file. The values of all registered items are written to disk.

void setCurrentGroup (const QString &group) | ### setCurrentGroup

[static]

Set the config file group for subsequent addItem() calls. It is valid until setCurrentGroup() is called with a new argument. Call this before you add any items. The default value is "No Group".

void addItem (KPrefsItem *) | ### addItem

Register a custom KPrefsItem.

void addItemBool (const QString &key,bool *reference, bool defaultValue=false) | ### addItemBool

Register an item of type bool.

Parameters:

key | Key used in config file.
reference | Pointer to the variable, which is set by readConfig() and setDefaults() calls and read by writeConfig() calls.
defaultValue | Default value, which is used by setDefaults() and when the config file does not yet contain the key of this item.

void addItemInt (const QString &key,int *reference, int defaultValue=0) | ### addItemInt

Register an item of type int.

Parameters:

key | Key used in config file.
reference | Pointer to the variable, which is set by readConfig() and setDefaults() calls and read by writeConfig() calls.
defaultValue | Default value, which is used by setDefaults() and when the config file does not yet contain the key of this item.

void addItemColor (const QString &key,QColor *reference, const QColor &defaultValue=QColor(128,128,128)) | ### addItemColor

Register an item of type QColor.

Parameters:

key | Key used in config file.
reference | Pointer to the variable, which is set by readConfig() and setDefaults() calls and read by writeConfig() calls.
defaultValue | Default value, which is used by setDefaults() and when the config file does not yet contain the key of this item.

void addItemFont (const QString &key,QFont *reference, const QFont &defaultValue=QFont("helvetica",12)) | ### addItemFont

Register an item of type QFont.

Parameters:

key | Key used in config file.
reference | Pointer to the variable, which is set by readConfig() and setDefaults() calls and read by writeConfig() calls.
defaultValue | Default value, which is used by setDefaults() and when the config file does not yet contain the key of this item.

void addItemString (const QString &key,QString *reference, const QString &defaultValue="") | ### addItemString

Register an item of type QString.

Parameters:

key | Key used in config file.
reference | Pointer to the variable, which is set by readConfig() and setDefaults() calls and read by writeConfig() calls.
defaultValue | Default value, which is used by setDefaults() and when the config file does not yet contain the key of this item.

void addItemStringList (const QString &key,QStringList *reference, const QStringList &defaultValue=QStringList()) | ### addItemStringList

Register an item of type QStringList.

Parameters:

key | Key used in config file.
reference | Pointer to the variable, which is set by readConfig() and setDefaults() calls and read by writeConfig() calls.
defaultValue | Default value, which is used by setDefaults() and when the config file does not yet contain the key of this item.

void addItemIntList (const QString &key,QValueList *reference, const QValueList &defaultValue=QValueList()) | ### addItemIntList

Register an item of type QValueList.

Parameters:

key | Key used in config file.
reference | Pointer to the variable, which is set by readConfig() and setDefaults() calls and read by writeConfig() calls.
defaultValue | Default value, which is used by setDefaults() and when the config file does not yet contain the key of this item.

void usrSetDefaults () | ### usrSetDefaults

[protected virtual]

Implemented by subclasses that use special defaults.

void usrReadConfig () | ### usrReadConfig

[protected virtual]

Implemented by subclasses that read special config values.

void usrWriteConfig () | ### usrWriteConfig

[protected virtual]

Implemented by subclasses that write special config values.

KConfig * config () | ### config

[protected const]

Return the KConfig object used for reading and writing the settings.

static QString * mCurrentGroup | ### mCurrentGroup

[private]

KConfig * mConfig | ### mConfig

[private]

QPtrList<KPrefsItem> mItems | ### mItems

[private]

- Author: Cornelius Schumacher
- Generated: sheldonl on shugaru on Thu Oct 3 22:56:49 2002, using kdoc 2.0a53.