QSettings(3qt)

NAME
QSettings - Persistent platform-independent application settings

SYNOPSIS
#include <qsettings.h>

  Public Members
enum Format { Native = 0, Ini }
enum System { Unix = 0, Windows, Mac }
enum Scope { User, Global }
QSettings ()
QSettings ( Format format )
~QSettings ()
bool writeEntry ( const QString & key, bool value )
bool writeEntry ( const QString & key, double value )
bool writeEntry ( const QString & key, int value )
bool writeEntry ( const QString & key, const QString & value )
bool writeEntry ( const QString & key, const QStringList & value )
bool writeEntry ( const QString & key, const QStringList & value, const QChar & separator )
QStringList entryList ( const QString & key ) const
QStringList subkeyList ( const QString & key ) const
QStringList readListEntry ( const QString & key, bool * ok = 0 ) const
QStringList readListEntry ( const QString & key, const QChar & separator, bool * ok = 0 ) const
QString readEntry ( const QString & key, const QString & def = QString::null, bool * ok = 0 ) const
int readNumEntry ( const QString & key, int def = 0, bool * ok = 0 ) const
double readDoubleEntry ( const QString & key, double def = 0, bool * ok = 0 ) const
bool readBoolEntry ( const QString & key, bool def = 0, bool * ok = 0 ) const
bool removeEntry ( const QString & key )
void insertSearchPath ( System s, const QString & path )
void removeSearchPath ( System s, const QString & path )
void setPath ( const QString & domain, const QString & product, Scope scope = Global )
void beginGroup ( const QString & group )
void endGroup ()
void resetGroup ()
QString group () const

DESCRIPTION
The QSettings class provides persistent platform-independent application settings.

On Unix systems, QSettings uses text files to store settings. On Windows systems, QSettings uses the system registry. On Mac OS X, QSettings will behave as on Unix, and store to text files.

Each setting comprises an identifying key and the data associated with the key. A key is a unicode string which consists of two or more subkeys. A subkey is a slash, '/', followed by one or more unicode characters (excluding slashes, newlines, carriage returns and equals, '=', signs). The associated data, called the entry or value, may be a boolean, an integer, a double, a string or a list of strings. Entry strings may contain any unicode characters.

If you want to save and restore the entire desktop's settings, i.e. which applications are running, use QSettings to save the settings for each individual application and QSessionManager to save the desktop's session.

Example settings:

    /MyCompany/MyApplication/background color
    /MyCompany/MyApplication/foreground color
    /MyCompany/MyApplication/geometry/x
    /MyCompany/MyApplication/geometry/y
    /MyCompany/MyApplication/geometry/width
    /MyCompany/MyApplication/geometry/height
    /MyCompany/MyApplication/recent files/1
    /MyCompany/MyApplication/recent files/2
    /MyCompany/MyApplication/recent files/3
Each line above is a complete key, made up of subkeys.

A typical usage pattern for application startup:

    QSettings settings;
    settings.insertSearchPath( QSettings::Windows, "/MyCompany" );
    // No search path needed for Unix; see notes further on.
    // Use default values if the keys don't exist
    QString bgColor = settings.readEntry( "/MyApplication/background color", "white" );
    int width = settings.readNumEntry( "/MyApplication/geometry/width", 640 );
    // ...

A typical usage pattern for application exit or 'save preferences':

    QSettings settings;
    settings.insertSearchPath( QSettings::Windows, "/MyCompany" );
    // No search path needed for Unix; see notes further on.
    settings.writeEntry( "/MyApplication/background color", bgColor );
    settings.writeEntry( "/MyApplication/geometry/width", width );
    // ...

You can get a list of entry-holding keys by calling entryList(), and a list of key-holding keys using subkeyList().

    QStringList keys = entryList( "/MyApplication" );
    // keys contains 'background color' and 'foreground color'.
    QStringList keys = entryList( "/MyApplication/recent files" );
    // keys contains '1', '2' and '3'.
    QStringList subkeys = subkeyList( "/MyApplication" );
    // subkeys contains 'geometry' and 'recent files'
    QStringList subkeys = subkeyList( "/MyApplication/recent files" );
    // subkeys is empty.

If you wish to use a different search path call insertSearchPath() as often as necessary to add your preferred paths. Call removeSearchPath() to remove any unwanted paths.

Since settings for Windows are stored in the registry there are size limits as follows:

A subkey may not exceed 255 characters.

An entry's value may not exceed 16,300 characters.

All the values of a key (for example, all the 'recent files' subkeys values), may not exceed 65,535 characters.

These limitations are not enforced on Unix.

Notes for Unix Applications
There is no universally accepted place for storing application settings under Unix. In the examples the settings file will be searched for in the following directories: <ol type=1>

INSTALL/etc/settings

/opt/MyCompany/share/etc

/opt/MyCompany/share/MyApplication/etc

$HOME/.qt When reading settings the files are searched in the order shown above, with later settings overriding earlier settings. Files for which the user doesn't have read permission are ignored. When saving settings QSettings works in the order shown above, writing to the first settings file for which the user has write permission. (INSTALL is the directory where Qt was installed. This can be modified by using the configure script's -prefix argument )

If you want to put the settings in a particular place in the filesystem you could do this:

    settings.insertSearchPath( QSettings::Unix, "/opt/MyCompany/share" );

But in practice you may prefer not to use a search path for Unix. For example the following code:

    settings.writeEntry( "/MyApplication/geometry/width", width );
will end up writing the "geometry/width" setting to the file $HOME/.qt/myapplicationrc (assuming that the application is being run by an ordinary user, i.e. not by root).

For cross-platform applications you should ensure that the Windows size limitations are not exceeded.

See also Input/Output and Networking and Miscellaneous Classes.

  Member Type Documentation

QSettings::Format

QSettings::Native - Store the settings in a platform dependent location

QSettings::Ini - Store the settings in a text file

QSettings::Scope

QSettings::Global - Save settings as global as possible

QSettings::User - Save settings in user space

QSettings::System

QSettings::Mac - Macintosh execution environments

QSettings::Unix - Mac OS X, Unix, Linux and Unix-like execution environments

QSettings::Windows - Windows execution environments

MEMBER FUNCTION DOCUMENTATION

QSettings::QSettings ()
Creates a settings object.

QSettings::QSettings ( Format format )
Creates a settings object. If format is 'Ini' the settings will be stored in a text file, using the Unix strategy (see above). If format is 'Native', the settings will be stored in a platform specific way (ie. the Windows registry).

QSettings::~QSettings ()
Destroys the settings object. All modifications made to the settings will automatically be saved.

void QSettings::beginGroup ( const QString & group )
Appends group to the current key prefix.

void QSettings::endGroup ()
Undo previous calls to beginGroup(). Note that a single beginGroup("a/b/c") is undone by a single call to endGroup().

QStringList QSettings::entryList ( const QString & key ) const
Returns a list of the keys which contain entries under key. Does not return any keys that contain keys.

Example settings:

    /MyCompany/MyApplication/background color
    /MyCompany/MyApplication/foreground color
    /MyCompany/MyApplication/geometry/x
    /MyCompany/MyApplication/geometry/y
    /MyCompany/MyApplication/geometry/width
    /MyCompany/MyApplication/geometry/height

    QStringList keys = entryList( "/MyCompany/MyApplication" );
keys contains 'background color' and 'foreground color'. It does not contain 'geometry' because this key contains keys not entries.

To access the geometry values could either use subkeyList() to read the keys and then read each entry, or simply read each entry directly by specifying its full key, e.g." /MyCompany/MyApplication/geometry/y".

See also subkeyList().

QString QSettings::group () const
Returns the current key prefix, or a null string if there is no key prefix set.

See also beginGroup().

void QSettings::insertSearchPath ( System s, const QString & path )
Inserts path into the settings search path. The semantics of path depends on the system s.

When s is Windows and the execution environment is not Windows the function does nothing. Similarly when s is Unix and the execution environment is not Unix the function does nothing.

When s is Windows, and the execution environment is Windows, the search path list will be used as the first subfolder of the "Software" folder in the registry.

When reading settings the folders are searched forwards from the first folder (listed below) to the last, returning the first settings found, and ignoring any folders for which the user doesn't have read permission. <ol type=1>

HKEY_CURRENT_USER/Software/MyCompany/MyApplication

HKEY_LOCAL_MACHINE/Software/MyCompany/MyApplication

HKEY_CURRENT_USER/Software/MyApplication

HKEY_LOCAL_MACHINE/Software/MyApplication

  QSettings settings;
  settings.insertSearchPath( QSettings::Windows, "/MyCompany" );
  settings.writeEntry( "/MyApplication/Tip of the day", TRUE );
The code above will write the subkey "Tip of the day" into the first of the registry folders listed below that is found and for which the user has write permission. <ol type=1>

HKEY_LOCAL_MACHINE/Software/MyCompany/MyApplication

HKEY_CURRENT_USER/Software/MyCompany/MyApplication

HKEY_LOCAL_MACHINE/Software/MyApplication

HKEY_CURRENT_USER/Software/MyApplication If a setting is found in the HKEY_CURRENT_USER space, this setting is overwritten independently of write permissions in the HKEY_LOCAL_MACHINE space.

When s is Unix, and the execution environment is Unix, the search path list will be used when trying to determine a suitable filename for reading and writing settings files. By default, there are two entries in the search path:

<ol type=1>

INSTALL/etc - where INSTALL is the directory where Qt was installed.

$HOME/.qt/ - where $HOME is the user's home directory.

All insertions into the search path will go before $HOME/.qt/. For example:

  QSettings settings;
  settings.insertSearchPath( QSettings::Unix, "/opt/MyCompany/share/etc" );
  settings.insertSearchPath( QSettings::Unix, "/opt/MyCompany/share/MyApplication/etc" );
  // ...
Will result in a search path of: <ol type=1>

INSTALL/etc

/opt/MyCompany/share/etc

/opt/MyCompany/share/MyApplication/etc

$HOME/.qt When reading settings the files are searched in the order shown above, with later settings overriding earlier settings. Files for which the user doesn't have read permission are ignored. When saving settings QSettings works in the order shown above, writing to the first settings file for which the user has write permission.

Settings under Unix are stored in files whose names are based on the first subkey of the key (not including the search path). The algorithm for creating names is essentially: lowercase the first subkey, replace spaces with underscores and add 'rc', e.g. /MyCompany/MyApplication/background color will be stored in myapplicationrc (assuming that /MyCompany is part of the search path).

See also removeSearchPath().

Example: chart/chartform.cpp.

bool QSettings::readBoolEntry ( const QString & key, bool def = 0, bool * ok = 0 ) const
Reads the entry specified by key, and returns a bool, or the default value, def, if the entry couldn't be read. If ok is non-null, *ok is set to TRUE if the key was read, FALSE otherwise.

See also readEntry(), readNumEntry(), readDoubleEntry(), writeEntry(), and removeEntry().

double QSettings::readDoubleEntry ( const QString & key, double def = 0, bool * ok = 0 ) const
Reads the entry specified by key, and returns a double, or the default value, def, if the entry couldn't be read. If ok is non-null, *ok is set to TRUE if the key was read, FALSE otherwise.

See also readEntry(), readNumEntry(), readBoolEntry(), writeEntry(), and removeEntry().

QString QSettings::readEntry ( const QString & key, const QString & def = QString::null, bool * ok = 0 ) const
Reads the entry specified by key, and returns a QString, or the default value, def, if the entry couldn't be read. If ok is non-null, *ok is set to TRUE if the key was read, FALSE otherwise.

See also readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), and removeEntry().

QStringList QSettings::readListEntry ( const QString & key, bool * ok = 0 ) const
Reads the entry specified by key as a string. If ok is not 0, *ok is set to TRUE if the key was read, otherwise *ok is set to FALSE.

Note that if you want to iterate over the list, you should iterate over a copy, e.g.

    QStringList list = mySettings.readListEntry( "recentfiles" );
    QStringList::Iterator it = list.begin();
    while( it != list.end() ) {
        myProcessing( *it );
        ++it;
    }

See also readEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), removeEntry(), and QStringList::split().

QStringList QSettings::readListEntry ( const QString & key, const QChar & separator, bool * ok = 0 ) const
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Reads the entry specified by key as a string. The separator is used to create a QStringList by calling QStringList::split(separator, entry). If ok is not 0: *ok is set to TRUE if the key was read, otherwise *ok is set to FALSE.

Note that if you want to iterate over the list, you should iterate over a copy, e.g.

    QStringList list = mySettings.readListEntry( "size", " " );
    QStringList::Iterator it = list.begin();
    while( it != list.end() ) {
        myProcessing( *it );
        ++it;
    }

See also readEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), removeEntry(), and QStringList::split().

int QSettings::readNumEntry ( const QString & key, int def = 0, bool * ok = 0 ) const
Reads the entry specified by key, and returns an integer, or the default value, def, if the entry couldn't be read. If ok is non-null, *ok is set to TRUE if the key was read, FALSE otherwise.

See also readEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), and removeEntry().

bool QSettings::removeEntry ( const QString & key )
Removes the entry specified by key.

Returns TRUE if the entry existed and was removed; otherwise returns FALSE.

See also readEntry() and writeEntry().

void QSettings::removeSearchPath ( System s, const QString & path )
Removes all occurrences of path (using exact matching) from the settings search path for system s. Note that the default search paths cannot be removed.

See also insertSearchPath().

void QSettings::resetGroup ()
Set the current key prefix to the empty string.

void QSettings::setPath ( const QString & domain, const QString & product, Scope scope = Global )
Insert platform-dependent paths from platform-independent information.

The domain should be an Internet domain name controlled by the producer of the software, eg. Trolltech products use "trolltech.com".

The product should be the official name of the product.

The scope should be QSettings::User for user-specific settings, or QSettings::Global for system-wide settings (generally these will be read-only to many users).

QStringList QSettings::subkeyList ( const QString & key ) const
Returns a list of the keys which contain keys under key. Does not return any keys that contain entries.

Example settings:

    /MyCompany/MyApplication/background color
    /MyCompany/MyApplication/foreground color
    /MyCompany/MyApplication/geometry/x
    /MyCompany/MyApplication/geometry/y
    /MyCompany/MyApplication/geometry/width
    /MyCompany/MyApplication/geometry/height
    /MyCompany/MyApplication/recent files/1
    /MyCompany/MyApplication/recent files/2
    /MyCompany/MyApplication/recent files/3

    QStringList keys = subkeyList( "/MyCompany/MyApplication" );
keys contains 'geometry' and 'recent files'. It does not contain 'background color' or 'foreground color' because they are keys which contain entries not keys. To get a list of keys that have values rather than subkeys use entryList().

See also entryList().

bool QSettings::writeEntry ( const QString & key, bool value )
Writes the boolean entry value into key key. The key is created if it doesn't exist. Any previous value is overwritten by value.

If an error occurs the settings are left unchanged and FALSE is returned; otherwise TRUE is returned.

See also readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), and removeEntry().

Example: chart/chartform.cpp.

bool QSettings::writeEntry ( const QString & key, double value )
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Writes the double entry value into key key. The key is created if it doesn't exist. Any previous value is overwritten by value.

If an error occurs the settings are left unchanged and FALSE is returned; otherwise TRUE is returned.

See also readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), and removeEntry().

bool QSettings::writeEntry ( const QString & key, int value )
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Writes the integer entry value into key key. The key is created if it doesn't exist. Any previous value is overwritten by value.

If an error occurs the settings are left unchanged and FALSE is returned; otherwise TRUE is returned.

See also readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), and removeEntry().

bool QSettings::writeEntry ( const QString & key, const QString & value )
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Writes the string entry value into key key. The key is created if it doesn't exist. Any previous value is overwritten by value. If value is an empty string or a null string the key's value will be an empty string.

If an error occurs the settings are left unchanged and FALSE is returned; otherwise TRUE is returned.

See also readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), and removeEntry().

bool QSettings::writeEntry ( const QString & key, const QStringList & value )
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Writes the string list entry value into key key. The key is created if it doesn't exist. Any previous value is overwritten by value.

If an error occurs the settings are left unchanged and FALSE is returned; otherwise returns TRUE.

See also readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), and removeEntry().

bool QSettings::writeEntry ( const QString & key, const QStringList & value, const QChar & separator )
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Writes the string list entry value into key key. The key is created if it doesn't exist. Any previous value is overwritten by value. The list is stored as a sequence of strings separated by separator, so none of the strings in the list should contain the separator. If the list is empty or null the key's value will be an empty string.

If an error occurs the settings are left unchanged and FALSE is returned; otherwise returns TRUE.

See also readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), and removeEntry().

SEE ALSO
http://doc.trolltech.com/qsettings.html http://www.trolltech.com/faq/tech.html

COPYRIGHT
Copyright 1992-2001 Trolltech AS, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.

AUTHOR
Generated automatically from the source code.

BUGS
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you.

The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech.

If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qsettings.3qt) and the Qt version (3.1.0-b2).