QSettings Class Reference

The QSettings class provides persistent platform-independent application settings.

  1. #include <QSettings>

Inherits: QObject.

Note: All functions in this class are reentrant, but registerFormat() is also thread-safe.

Detailed Description

The QSettings class provides persistent platform-independent application settings.

Users normally expect an application to remember its settings (window sizes and positions, options, etc.) across sessions. This information is often stored in the system registry on Windows, and in XML preferences files on Mac OS X. On Unix systems, in the absence of a standard, many applications (including the KDE applications) use INI text files.

QSettings is an abstraction around these technologies, enabling you to save and restore application settings in a portable manner. It also supports custom storage formats.

QSettings's API is based on QVariant, allowing you to save most value-based types, such as QString, QRect, and QImage, with the minimum of effort.

If all you need is a non-persistent memory-based structure, consider using QMap<QString, QVariant> instead.

Basic Usage

When creating a QSettings object, you must pass the name of your company or organization as well as the name of your application. For example, if your product is called Star Runner and your company is called MySoft, you would construct the QSettings object as follows:

  1.     QSettings settings("MySoft", "Star Runner");

QSettings objects can be created either on the stack or on the heap (i.e. using new). Constructing and destroying a QSettings object is very fast.

If you use QSettings from many places in your application, you might want to specify the organization name and the application name using QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName(), and then use the default QSettings constructor:

  1.     QCoreApplication::setOrganizationName("MySoft");
  2.     QCoreApplication::setOrganizationDomain("mysoft.com");
  3.     QCoreApplication::setApplicationName("Star Runner");
  4.     ...
  5.     QSettings settings;

(Here, we also specify the organization's Internet domain. When the Internet domain is set, it is used on Mac OS X instead of the organization name, since Mac OS X applications conventionally use Internet domains to identify themselves. If no domain is set, a fake domain is derived from the organization name. See the Platform-Specific Notes below for details.)

QSettings stores settings. Each setting consists of a QString that specifies the setting's name (the key) and a QVariant that stores the data associated with the key. To write a setting, use setValue(). For example:

  1.     settings.setValue("editor/wrapMargin", 68);

If there already exists a setting with the same key, the existing value is overwritten by the new value. For efficiency, the changes may not be saved to permanent storage immediately. (You can always call sync() to commit your changes.)

You can get a setting's value back using value():

  1.     int margin = settings.value("editor/wrapMargin").toInt();

If there is no setting with the specified name, QSettings returns a null QVariant (which can be converted to the integer 0). You can specify another default value by passing a second argument to value():

  1.     int margin = settings.value("editor/wrapMargin", 80).toInt();

To test whether a given key exists, call contains(). To remove the setting associated with a key, call remove(). To obtain the list of all keys, call allKeys(). To remove all keys, call clear().

QVariant and GUI Types

Because QVariant is part of the QtCore library, it cannot provide conversion functions to data types such as QColor, QImage, and QPixmap, which are part of QtGui. In other words, there is no toColor(), toImage(), or toPixmap() functions in QVariant.

Instead, you can use the QVariant::value() or the qVariantValue() template function. For example:

  1. QSettings settings("MySoft", "Star Runner");
  2. QColor color = settings.value("DataPump/bgcolor").value<QColor>();

The inverse conversion (e.g., from QColor to QVariant) is automatic for all data types supported by QVariant, including GUI-related types:

  1. QSettings settings("MySoft", "Star Runner");
  2. QColor color = palette().background().color();
  3. settings.setValue("DataPump/bgcolor", color);

Custom types registered using qRegisterMetaType() and qRegisterMetaTypeStreamOperators() can be stored using QSettings.

Section and Key Syntax

Setting keys can contain any Unicode characters. The Windows registry and INI files use case-insensitive keys, whereas the Carbon Preferences API on Mac OS X uses case-sensitive keys. To avoid portability problems, follow these simple rules:

  1. Always refer to the same key using the same case. For example, if you refer to a key as "text fonts" in one place in your code, don't refer to it as "Text Fonts" somewhere else.
  2. Avoid key names that are identical except for the case. For example, if you have a key called "MainWindow", don't try to save another key as "mainwindow".
  3. Do not use slashes ('/' and '\') in section or key names; the backslash character is used to separate sub keys (see below). On windows '\' are converted by QSettings to '/', which makes them identical.

You can form hierarchical keys using the '/' character as a separator, similar to Unix file paths. For example:

  1.     settings.setValue("mainwindow/size", win->size());
  2.     settings.setValue("mainwindow/fullScreen", win->isFullScreen());
  3.     settings.setValue("outputpanel/visible", panel->isVisible());

If you want to save or restore many settings with the same prefix, you can specify the prefix using beginGroup() and call endGroup() at the end. Here's the same example again, but this time using the group mechanism:

  1.     settings.beginGroup("mainwindow");
  2.     settings.setValue("size", win->size());
  3.     settings.setValue("fullScreen", win->isFullScreen());
  4.     settings.endGroup();
  5.  
  6.     settings.beginGroup("outputpanel");
  7.     settings.setValue("visible", panel->isVisible());
  8.     settings.endGroup();

If a group is set using beginGroup(), the behavior of most functions changes consequently. Groups can be set recursively.

In addition to groups, QSettings also supports an "array" concept. See beginReadArray() and beginWriteArray() for details.

Fallback Mechanism

Let's assume that you have created a QSettings object with the organization name MySoft and the application name Star Runner. When you look up a value, up to four locations are searched in that order:

  1. a user-specific location for the Star Runner application
  2. a user-specific location for all applications by MySoft
  3. a system-wide location for the Star Runner application
  4. a system-wide location for all applications by MySoft

(See Platform-Specific Notes below for information on what these locations are on the different platforms supported by Qt.)

If a key cannot be found in the first location, the search goes on in the second location, and so on. This enables you to store system-wide or organization-wide settings and to override them on a per-user or per-application basis. To turn off this mechanism, call setFallbacksEnabled(false).

Although keys from all four locations are available for reading, only the first file (the user-specific location for the application at hand) is accessible for writing. To write to any of the other files, omit the application name and/or specify QSettings::SystemScope (as opposed to QSettings::UserScope, the default).

Let's see with an example:

  1.     QSettings obj1("MySoft", "Star Runner");
  2.     QSettings obj2("MySoft");
  3.     QSettings obj3(QSettings::SystemScope, "MySoft", "Star Runner");
  4.     QSettings obj4(QSettings::SystemScope, "MySoft");

The table below summarizes which QSettings objects access which location. "X" means that the location is the main location associated to the QSettings object and is used both for reading and for writing; "o" means that the location is used as a fallback when reading.

Locations obj1 obj2 obj3 obj4
1. User, Application X
2. User, Organizationo X
3. System, Applicationo X
4. System, Organizationooo X

The beauty of this mechanism is that it works on all platforms supported by Qt and that it still gives you a lot of flexibility, without requiring you to specify any file names or registry paths.

If you want to use INI files on all platforms instead of the native API, you can pass QSettings::IniFormat as the first argument to the QSettings constructor, followed by the scope, the organization name, and the application name:

  1.     QSettings settings(QSettings::IniFormat, QSettings::UserScope,
  2.                        "MySoft", "Star Runner");

The Settings Editor example lets you experiment with different settings location and with fallbacks turned on or off.

Restoring the State of a GUI Application

QSettings is often used to store the state of a GUI application. The following example illustrates how to use QSettings to save and restore the geometry of an application's main window.

  1. void MainWindow::writeSettings()
  2.  {
  3.     QSettings settings("Moose Soft", "Clipper");
  4.  
  5.     settings.beginGroup("MainWindow");
  6.     settings.setValue("size", size());
  7.     settings.setValue("pos", pos());
  8.     settings.endGroup();
  9. }
  10.  
  11. void MainWindow::readSettings()
  12.  {
  13.     QSettings settings("Moose Soft", "Clipper");
  14.  
  15.     settings.beginGroup("MainWindow");
  16.     resize(settings.value("size", QSize(400, 400)).toSize());
  17.     move(settings.value("pos", QPoint(200, 200)).toPoint());
  18.     settings.endGroup();
  19. }

See Window Geometry for a discussion on why it is better to call QWidget::resize() and QWidget::move() rather than QWidget::setGeometry() to restore a window's geometry.

The readSettings() and writeSettings() functions must be called from the main window's constructor and close event handler as follows:

  1. MainWindow::MainWindow()
  2.  {
  3.     ...
  4.     readSettings();
  5. }
  6.  
  7. void MainWindow::closeEvent(QCloseEvent *event)
  8.  {
  9.     if (userReallyWantsToQuit())  {
  10.         writeSettings();
  11.         event->accept();
  12.     } else  {
  13.         event->ignore();
  14.     }
  15. }

See the Application example for a self-contained example that uses QSettings.

Accessing Settings from Multiple Threads or Processes Simultaneously

QSettings is reentrant. This means that you can use distinct QSettings object in different threads simultaneously. This guarantee stands even when the QSettings objects refer to the same files on disk (or to the same entries in the system registry). If a setting is modified through one QSettings object, the change will immediately be visible in any other QSettings objects that operate on the same location and that live in the same process.

QSettings can safely be used from different processes (which can be different instances of your application running at the same time or different applications altogether) to read and write to the same system locations. It uses advisory file locking and a smart merging algorithm to ensure data integrity. Note that sync() imports changes made by other processes (in addition to writing the changes from this QSettings).

Platform-Specific Notes

Locations Where Application Settings Are Stored

As mentioned in the Fallback Mechanism section, QSettings stores settings for an application in up to four locations, depending on whether the settings are user-specific or system-wide and whether the settings are application-specific or organization-wide. For simplicity, we're assuming the organization is called MySoft and the application is called Star Runner.

On Unix systems, if the file format is NativeFormat, the following files are used by default:

  1. $HOME/.config/MySoft/Star Runner.conf (Qt for Embedded Linux: $HOME/Settings/MySoft/Star Runner.conf)
  2. $HOME/.config/MySoft.conf (Qt for Embedded Linux: $HOME/Settings/MySoft.conf)
  3. /etc/xdg/MySoft/Star Runner.conf
  4. /etc/xdg/MySoft.conf

On Mac OS X versions 10.2 and 10.3, these files are used by default:

  1. $HOME/Library/Preferences/com.MySoft.Star Runner.plist
  2. $HOME/Library/Preferences/com.MySoft.plist
  3. /Library/Preferences/com.MySoft.Star Runner.plist
  4. /Library/Preferences/com.MySoft.plist

On Windows, NativeFormat settings are stored in the following registry paths:

  1. HKEY_CURRENT_USER\Software\MySoft\Star Runner
  2. HKEY_CURRENT_USER\Software\MySoft
  3. HKEY_LOCAL_MACHINE\Software\MySoft\Star Runner
  4. HKEY_LOCAL_MACHINE\Software\MySoft

Note: On Windows, for 32-bit programs running in WOW64 mode, settings are stored in the following registry path: HKEY_LOCAL_MACHINE\Software\WOW6432node.

If the file format is IniFormat, the following files are used on Unix and Mac OS X:

  1. $HOME/.config/MySoft/Star Runner.ini (Qt for Embedded Linux: $HOME/Settings/MySoft/Star Runner.ini)
  2. $HOME/.config/MySoft.ini (Qt for Embedded Linux: $HOME/Settings/MySoft.ini)
  3. /etc/xdg/MySoft/Star Runner.ini
  4. /etc/xdg/MySoft.ini

On Windows, the following files are used:

  1. %APPDATA%\MySoft\Star Runner.ini
  2. %APPDATA%\MySoft.ini
  3. %COMMON_APPDATA%\MySoft\Star Runner.ini
  4. %COMMON_APPDATA%\MySoft.ini

The %APPDATA% path is usually C:\Documents and Settings\User Name\Application Data; the %COMMON_APPDATA% path is usually C:\Documents and Settings\All Users\Application Data.

The paths for the .ini and .conf files can be changed using setPath(). On Unix and Mac OS X, the user can override them by by setting the XDG_CONFIG_HOME environment variable; see setPath() for details.

Accessing INI and .plist Files Directly

Sometimes you do want to access settings stored in a specific file or registry path. On all platforms, if you want to read an INI file directly, you can use the QSettings constructor that takes a file name as first argument and pass QSettings::IniFormat as second argument. For example:

  1. QSettings settings("/home/petra/misc/myapp.ini",
  2.                    QSettings::IniFormat);

You can then use the QSettings object to read and write settings in the file.

On Mac OS X, you can access XML-based .plist files by passing QSettings::NativeFormat as second argument. For example:

  1. QSettings settings("/Users/petra/misc/myapp.plist",
  2.                    QSettings::NativeFormat);

Accessing the Windows Registry Directly

On Windows, QSettings lets you access settings that have been written with QSettings (or settings in a supported format, e.g., string data) in the system registry. This is done by constructing a QSettings object with a path in the registry and QSettings::NativeFormat.

For example:

  1. QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Office",
  2.                    QSettings::NativeFormat);

All the registry entries that appear under the specified path can be read or written through the QSettings object as usual (using forward slashes instead of backslashes). For example:

  1. settings.setValue("11.0/Outlook/Security/DontTrustInstalledFiles", 0);

Note that the backslash character is, as mentioned, used by QSettings to separate subkeys. As a result, you cannot read or write windows registry entries that contain slashes or backslashes; you should use a native windows API if you need to do so.

Accessing Common Registry Settings on Windows

On Windows, it is possible for a key to have both a value and subkeys. Its default value is accessed by using "Default" or "." in place of a subkey:

  1. settings.setValue("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy", "Milkyway");
  2. settings.setValue("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy\\Sun", "OurStar");
  3. settings.value("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy\\Default"); // returns "Milkyway"

On other platforms than Windows, "Default" and "." would be treated as regular subkeys.

Platform Limitations

While QSettings attempts to smooth over the differences between the different supported platforms, there are still a few differences that you should be aware of when porting your application:

  • The Windows system registry has the following limitations: A subkey may not exceed 255 characters, an entry's value may not exceed 16,383 characters, and all the values of a key may not exceed 65,535 characters. One way to work around these limitations is to store the settings using the IniFormat instead of the NativeFormat.
  • On Mac OS X, allKeys() will return some extra keys for global settings that apply to all applications. These keys can be read using value() but cannot be changed, only shadowed. Calling setFallbacksEnabled(false) will hide these global settings.
  • On Mac OS X, the CFPreferences API used by QSettings expects Internet domain names rather than organization names. To provide a uniform API, QSettings derives a fake domain name from the organization name (unless the organization name already is a domain name, e.g. OpenOffice.org). The algorithm appends ".com" to the company name and replaces spaces and other illegal characters with hyphens. If you want to specify a different domain name, call QCoreApplication::setOrganizationDomain(), QCoreApplication::setOrganizationName(), and QCoreApplication::setApplicationName() in your main() function and then use the default QSettings constructor. Another solution is to use preprocessor directives, for example:
    1. #ifdef Q_WS_MAC
    2.     QSettings settings("grenoullelogique.fr", "Squash");
    3. #else
    4.     QSettings settings("Grenoulle Logique", "Squash");
    5. #endif
  • On Unix and Mac OS X systems, the advisory file locking is disabled if NFS (or AutoFS or CacheFS) is detected to work around a bug in the NFS fcntl() implementation, which hangs forever if statd or lockd aren't running. Also, the locking isn't performed when accessing .plist files.

See also QVariant, QSessionManager, Settings Editor Example, and Application Example.

Public Types

Toggle detailsenum QSettings::

FormatFormat { NativeFormat , IniFormat , InvalidFormat , CustomFormat1 , CustomFormat2 , CustomFormat3 , CustomFormat4 , CustomFormat5 , CustomFormat6 , CustomFormat7 , CustomFormat8 , CustomFormat9 , CustomFormat10 , CustomFormat11 , CustomFormat12 , CustomFormat13 , CustomFormat14 , CustomFormat15 , CustomFormat16 32 ...} { NativeFormat , IniFormat , InvalidFormat , CustomFormat1 , CustomFormat2 , CustomFormat3 , CustomFormat4 , CustomFormat5 , CustomFormat6 , CustomFormat7 , CustomFormat8 , CustomFormat9 , CustomFormat10 , CustomFormat11 , CustomFormat12 , CustomFormat13 , CustomFormat14 , CustomFormat15 , CustomFormat16 32 }

This enum type specifies the storage format used by QSettings.

ConstantValueDescription
QSettings::NativeFormat 0 Store the settings using the most appropriate storage format for the platform. On Windows, this means the system registry; on Mac OS X, this means the CFPreferences API; on Unix, this means textual configuration files in INI format.
QSettings::IniFormat 1 Store the settings in INI files.
QSettings::InvalidFormat 16 Special value returned by registerFormat().

On Unix, NativeFormat and IniFormat mean the same thing, except that the file extension is different (.conf for NativeFormat, .ini for IniFormat).

The INI file format is a Windows file format that Qt supports on all platforms. In the absence of an INI standard, we try to follow what Microsoft does, with the following exceptions:

  • If you store types that QVariant can't convert to QString (e.g., QPoint, QRect, and QSize), Qt uses an @-based syntax to encode the type. For example:
    1. pos = @Point(100 100)

    To minimize compatibility issues, any @ that doesn't appear at the first position in the value or that isn't followed by a Qt type (Point, Rect, Size, etc.) is treated as a normal character.

  • Although backslash is a special character in INI files, most Windows applications don't escape backslashes (\) in file paths:
    1. windir = C:\Windows

    QSettings always treats backslash as a special character and provides no API for reading or writing such entries.

  • The INI file format has severe restrictions on the syntax of a key. Qt works around this by using % as an escape character in keys. In addition, if you save a top-level setting (a key with no slashes in it, e.g., "someKey"), it will appear in the INI file's "General" section. To avoid overwriting other keys, if you save something using the a key such as "General/someKey", the key will be located in the "%General" section, not in the "General" section.
  • Following the philosophy that we should be liberal in what we accept and conservative in what we generate, QSettings will accept Latin-1 encoded INI files, but generate pure ASCII files, where non-ASCII values are encoded using standard INI escape sequences. To make the INI files more readable (but potentially less compatible), call setIniCodec().

See also registerFormat() and setPath().

Look up this member in the source code.

Toggle detailsenum QSettings::

ScopeScope { UserScope , SystemScope , User , Global SystemScope ...} { UserScope , SystemScope , User , Global SystemScope }

This enum specifies whether settings are user-specific or shared by all users of the same system.

ConstantValueDescription
QSettings::UserScope 0 Store settings in a location specific to the current user (e.g., in the user's home directory).
QSettings::SystemScope 1 Store settings in a global location, so that all users on the same machine access the same set of settings.

See also setPath().

Look up this member in the source code.

Toggle detailsenum QSettings::

StatusStatus { NoError , AccessError , FormatError 2 ...} { NoError , AccessError , FormatError 2 }

The following status values are possible:

ConstantValueDescription
QSettings::NoError 0 No error occurred.
QSettings::AccessError 1 An access error occurred (e.g. trying to write to a read-only file).
QSettings::FormatError 2 A format error occurred (e.g. loading a malformed INI file).

See also status().

Look up this member in the source code.

Toggle detailsenum QSettings::

SystemSystem { Unix , Windows , Mac 2 ...} { Unix , Windows , Mac 2 }Qt3-support

ConstantValueDescription
QSettings::Unix 0 Unix systems (X11 and Embedded Linux)
QSettings::Windows 1 Microsoft Windows systems
QSettings::Mac 2 Mac OS X systems

See also insertSearchPath() and removeSearchPath().

Look up this member in the source code.

Types

Toggle detailstypedef QSettings ReadFuncReadFunc

Typedef for a pointer to a function with the following signature:

  1. bool myReadFunc(QIODevice &device, QSettings::SettingsMap &map);

ReadFunc is used in registerFormat() as a pointer to a function that reads a set of key/value pairs. ReadFunc should read all the options in one pass, and return all the settings in the SettingsMap container, which is initially empty.

See also WriteFunc and registerFormat().

Look up this member in the source code.

Toggle detailstypedef QSettings SettingsMapSettingsMap

Typedef for QMap<QString, QVariant>.

See also registerFormat().

Look up this member in the source code.

Toggle detailstypedef QSettings WriteFuncWriteFunc

Typedef for a pointer to a function with the following signature:

  1. bool myWriteFunc(QIODevice &device, const QSettings::SettingsMap &map);

WriteFunc is used in registerFormat() as a pointer to a function that writes a set of key/value pairs. WriteFunc is only called once, so you need to output the settings in one go.

See also ReadFunc and registerFormat().

Look up this member in the source code.

    Public Functions

    Toggle details QSettings

    QSettingsQSettings ( const QString &organization , const QString &application=QString() , QObject *parent=0 ...) ( const QString &organization , const QString &application=QString() , QObject *parent=0 )

    Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent.

    Example:

    1.                     QSettings settings("Moose Tech", "Facturo-Pro");

    The scope is set to QSettings::UserScope, and the format is set to QSettings::NativeFormat (i.e. calling setDefaultFormat() before calling this constructor has no effect).

    See also setDefaultFormat() and Fallback Mechanism.

    Look up this member in the source code.

    Toggle details QSettings

    QSettingsQSettings ( Scope scope , const QString &organization , const QString &application=QString() , QObject *parent=0 ...) ( Scope scope , const QString &organization , const QString &application=QString() , QObject *parent=0 )

    Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent.

    If scope is QSettings::UserScope, the QSettings object searches user-specific settings first, before it searches system-wide settings as a fallback. If scope is QSettings::SystemScope, the QSettings object ignores user-specific settings and provides access to system-wide settings.

    The storage format is set to QSettings::NativeFormat (i.e. calling setDefaultFormat() before calling this constructor has no effect).

    If no application name is given, the QSettings object will only access the organization-wide locations.

    See also setDefaultFormat().

    Look up this member in the source code.

    Toggle details QSettings

    QSettingsQSettings ( Format format , Scope scope , const QString &organization , const QString &application=QString() , QObject *parent=0 ...) ( Format format , Scope scope , const QString &organization , const QString &application=QString() , QObject *parent=0 )

    Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent.

    If scope is QSettings::UserScope, the QSettings object searches user-specific settings first, before it searches system-wide settings as a fallback. If scope is QSettings::SystemScope, the QSettings object ignores user-specific settings and provides access to system-wide settings.

    If format is QSettings::NativeFormat, the native API is used for storing settings. If format is QSettings::IniFormat, the INI format is used.

    If no application name is given, the QSettings object will only access the organization-wide locations.

    Look up this member in the source code.

    Toggle details QSettings

    QSettingsQSettings ( const QString &fileName , Format format , QObject *parent=0 ...) ( const QString &fileName , Format format , QObject *parent=0 )

    Constructs a QSettings object for accessing the settings stored in the file called fileName, with parent parent. If the file doesn't already exist, it is created.

    If format is QSettings::NativeFormat, the meaning of fileName depends on the platform. On Unix, fileName is the name of an INI file. On Mac OS X, fileName is the name of a .plist file. On Windows, fileName is a path in the system registry.

    If format is QSettings::IniFormat, fileName is the name of an INI file.

    Warning: This function is provided for convenience. It works well for accessing INI or .plist files generated by Qt, but might fail on some syntaxes found in such files originated by other programs. In particular, be aware of the following limitations:

    • QSettings provides no way of reading INI "path" entries, i.e., entries with unescaped slash characters. (This is because these entries are ambiguous and cannot be resolved automatically.)
    • In INI files, QSettings uses the @ character as a metacharacter in some contexts, to encode Qt-specific data types (e.g., @Rect), and might therefore misinterpret it when it occurs in pure INI files.

    See also fileName().

    Look up this member in the source code.

    Toggle details QSettings

    QSettingsQSettings ( QObject *parent=0 ) ( QObject *parent=0 )

    Constructs a QSettings object for accessing settings of the application and organization set previously with a call to QCoreApplication::setOrganizationName(), QCoreApplication::setOrganizationDomain(), and QCoreApplication::setApplicationName().

    The scope is QSettings::UserScope and the format is defaultFormat() (QSettings::NativeFormat by default). Use setDefaultFormat() before calling this constructor to change the default format used by this constructor.

    The code

    1.                     QSettings settings("Moose Soft", "Facturo-Pro");

    is equivalent to

    1.                     QCoreApplication::setOrganizationName("Moose Soft");
    2. QCoreApplication::setApplicationName("Facturo-Pro");
    3. QSettings settings;

    If QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName() has not been previously called, the QSettings object will not be able to read or write any settings, and status() will return AccessError.

    On Mac OS X, if both a name and an Internet domain are specified for the organization, the domain is preferred over the name. On other platforms, the name is preferred over the domain.

    See also QCoreApplication::setOrganizationName(), QCoreApplication::setOrganizationDomain(), QCoreApplication::setApplicationName(), and setDefaultFormat().

    Look up this member in the source code.

    Toggle details QSettings

    ~QSettings~QSettings () ()

    Destroys the QSettings object.

    Any unsaved changes will eventually be written to permanent storage.

    See also sync().

    Look up this member in the source code.

    Toggle details QStringList QSettings

    allKeysallKeys () ()const

    Returns a list of all keys, including subkeys, that can be read using the QSettings object.

    Example:

    1.                     QSettings settings;
    2. settings.setValue("fridge/color", Qt::white);
    3. settings.setValue("fridge/size", QSize(32, 96));
    4. settings.setValue("sofa", true);
    5. settings.setValue("tv", false);
    6.  
    7. QStringList keys = settings.allKeys();
    8. // keys: ["fridge/color", "fridge/size", "sofa", "tv"]

    If a group is set using beginGroup(), only the keys in the group are returned, without the group prefix:

    1. settings.beginGroup("fridge");
    2. keys = settings.allKeys();
    3. // keys: ["color", "size"]

    See also childGroups() and childKeys().

    Look up this member in the source code.

    Toggle details QString QSettings

    applicationNameapplicationName () ()const

    Returns the application name used for storing the settings.

    See also QCoreApplication::applicationName(), format(), scope(), and organizationName().

    Look up this member in the source code.

    Toggle details void QSettings

    beginGroupbeginGroup ( const QString &prefix ) ( const QString &prefix )

    Appends prefix to the current group.

    The current group is automatically prepended to all keys specified to QSettings. In addition, query functions such as childGroups(), childKeys(), and allKeys() are based on the group. By default, no group is set.

    Groups are useful to avoid typing in the same setting paths over and over. For example:

    1. settings.beginGroup("mainwindow");
    2. settings.setValue("size", win->size());
    3. settings.setValue("fullScreen", win->isFullScreen());
    4. settings.endGroup();
    5.  
    6. settings.beginGroup("outputpanel");
    7. settings.setValue("visible", panel->isVisible());
    8. settings.endGroup();

    This will set the value of three settings:

    • mainwindow/size
    • mainwindow/fullScreen
    • outputpanel/visible

    Call endGroup() to reset the current group to what it was before the corresponding beginGroup() call. Groups can be nested.

    See also endGroup() and group().

    Look up this member in the source code.

    Toggle details int QSettings

    beginReadArraybeginReadArray ( const QString &prefix ) ( const QString &prefix )

    Adds prefix to the current group and starts reading from an array. Returns the size of the array.

    Example:

    1. struct Login  {
    2.     QString userName;
    3.     QString password;
    4. };
    5. QList<Login> logins;
    6. ...
    7.  
    8. QSettings settings;
    9. int size = settings.beginReadArray("logins");
    10. for (int i = 0; i < size; ++i)  {
    11.     settings.setArrayIndex(i);
    12.     Login login;
    13.     login.userName = settings.value("userName").toString();
    14.     login.password = settings.value("password").toString();
    15.     logins.append(login);
    16. }
    17. settings.endArray();

    Use beginWriteArray() to write the array in the first place.

    See also beginWriteArray(), endArray(), and setArrayIndex().

    Look up this member in the source code.

    Toggle details void QSettings

    beginWriteArraybeginWriteArray ( const QString &prefix , int size=-1 ...) ( const QString &prefix , int size=-1 )

    Adds prefix to the current group and starts writing an array of size size. If size is -1 (the default), it is automatically determined based on the indexes of the entries written.

    If you have many occurrences of a certain set of keys, you can use arrays to make your life easier. For example, let's suppose that you want to save a variable-length list of user names and passwords. You could then write:

    1. struct Login  {
    2.     QString userName;
    3.     QString password;
    4. };
    5. QList<Login> logins;
    6. ...
    7.  
    8. QSettings settings;
    9. settings.beginWriteArray("logins");
    10. for (int i = 0; i < logins.size(); ++i)  {
    11.     settings.setArrayIndex(i);
    12.     settings.setValue("userName", list.at(i).userName);
    13.     settings.setValue("password", list.at(i).password);
    14. }
    15. settings.endArray();

    The generated keys will have the form

    • logins/size
    • logins/1/userName
    • logins/1/password
    • logins/2/userName
    • logins/2/password
    • logins/3/userName
    • logins/3/password
    • ...

    To read back an array, use beginReadArray().

    See also beginReadArray(), endArray(), and setArrayIndex().

    Look up this member in the source code.

    Toggle details QStringList QSettings

    childGroupschildGroups () ()const

    Returns a list of all key top-level groups that contain keys that can be read using the QSettings object.

    Example:

    1.                     QSettings settings;
    2. settings.setValue("fridge/color", Qt::white);
    3. settings.setValue("fridge/size", QSize(32, 96));
    4. settings.setValue("sofa", true);
    5. settings.setValue("tv", false);
    6.  
    7. QStringList groups = settings.childGroups();
    8. // group: ["fridge"]

    If a group is set using beginGroup(), the first-level keys in that group are returned, without the group prefix.

    1. settings.beginGroup("fridge");
    2. groups = settings.childGroups();
    3. // groups: []

    You can navigate through the entire setting hierarchy using childKeys() and childGroups() recursively.

    See also childKeys() and allKeys().

    Look up this member in the source code.

    Toggle details QStringList QSettings

    childKeyschildKeys () ()const

    Returns a list of all top-level keys that can be read using the QSettings object.

    Example:

    1.                     QSettings settings;
    2. settings.setValue("fridge/color", Qt::white);
    3. settings.setValue("fridge/size", QSize(32, 96));
    4. settings.setValue("sofa", true);
    5. settings.setValue("tv", false);
    6.  
    7. QStringList keys = settings.childKeys();
    8. // keys: ["sofa", "tv"]

    If a group is set using beginGroup(), the top-level keys in that group are returned, without the group prefix:

    1. settings.beginGroup("fridge");
    2. keys = settings.childKeys();
    3. // keys: ["color", "size"]

    You can navigate through the entire setting hierarchy using childKeys() and childGroups() recursively.

    See also childGroups() and allKeys().

    Look up this member in the source code.

    Toggle details void QSettings

    clearclear () ()

    Removes all entries in the primary location associated to this QSettings object.

    Entries in fallback locations are not removed.

    If you only want to remove the entries in the current group(), use remove("") instead.

    See also remove() and setFallbacksEnabled().

    Look up this member in the source code.

    Toggle details bool QSettings

    containscontains ( const QString &key ) ( const QString &key )const

    Returns true if there exists a setting called key; returns false otherwise.

    If a group is set using beginGroup(), key is taken to be relative to that group.

    Note that the Windows registry and INI files use case-insensitive keys, whereas the Carbon Preferences API on Mac OS X uses case-sensitive keys. To avoid portability problems, see the Section and Key Syntax rules.

    See also value() and setValue().

    Look up this member in the source code.

    Toggle details Format QSettings

    defaultFormatdefaultFormat () () [static]

    Returns default file format used for storing settings for the QSettings(QObject *) constructor. If no default format is set, QSettings::NativeFormat is used.

    See also setDefaultFormat() and format().

    Look up this member in the source code.

    Toggle details void QSettings

    endArrayendArray () ()

    Closes the array that was started using beginReadArray() or beginWriteArray().

    See also beginReadArray() and beginWriteArray().

    Look up this member in the source code.

    Toggle details void QSettings

    endGroupendGroup () ()

    Resets the group to what it was before the corresponding beginGroup() call.

    Example:

    1. settings.beginGroup("alpha");
    2. // settings.group() == "alpha"
    3.  
    4. settings.beginGroup("beta");
    5. // settings.group() == "alpha/beta"
    6.  
    7. settings.endGroup();
    8. // settings.group() == "alpha"
    9.  
    10. settings.endGroup();
    11. // settings.group() == ""

    See also beginGroup() and group().

    Look up this member in the source code.

    Toggle details bool QSettings

    eventevent ( QEvent *event ) ( QEvent *event ) [virtual protected]

    Reimplemented from QObject::event().

    Look up this member in the source code.

    Toggle details bool QSettings

    fallbacksEnabledfallbacksEnabled () ()const

    Returns true if fallbacks are enabled; returns false otherwise.

    By default, fallbacks are enabled.

    See also setFallbacksEnabled().

    Look up this member in the source code.

    Toggle details QString QSettings

    fileNamefileName () ()const

    Returns the path where settings written using this QSettings object are stored.

    On Windows, if the format is QSettings::NativeFormat, the return value is a system registry path, not a file path.

    See also isWritable() and format().

    Look up this member in the source code.

    Toggle details Format QSettings

    formatformat () ()const

    Returns the format used for storing the settings.

    See also defaultFormat(), fileName(), scope(), organizationName(), and applicationName().

    Look up this member in the source code.

    Toggle details QString QSettings

    groupgroup () ()const

    Returns the current group.

    See also beginGroup() and endGroup().

    Look up this member in the source code.

    Toggle details QTextCodec * QSettings

    iniCodeciniCodec () ()const

    Returns the codec that is used for accessing INI files. By default, no codec is used, so a null pointer is returned.

    See also setIniCodec().

    Look up this member in the source code.

    Toggle details bool QSettings

    isWritableisWritable () ()const

    Returns true if settings can be written using this QSettings object; returns false otherwise.

    One reason why isWritable() might return false is if QSettings operates on a read-only file.

    Warning: This function is not perfectly reliable, because the file permissions can change at any time.

    See also fileName(), status(), and sync().

    Look up this member in the source code.

    Toggle details QString QSettings

    organizationNameorganizationName () ()const

    Returns the organization name used for storing the settings.

    See also QCoreApplication::organizationName(), format(), scope(), and applicationName().

    Look up this member in the source code.

    Toggle details Format QSettings

    registerFormatregisterFormat ( const QString &extension , ReadFunc readFunc , WriteFunc writeFunc , Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive ...) ( const QString &extension , ReadFunc readFunc , WriteFunc writeFunc , Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive ) [static]

    Registers a custom storage format. On success, returns a special Format value that can then be passed to the QSettings constructor. On failure, returns InvalidFormat.

    The extension is the file extension associated to the format (without the '.').

    The readFunc and writeFunc parameters are pointers to functions that read and write a set of key/value pairs. The QIODevice parameter to the read and write functions is always opened in binary mode (i.e., without the QIODevice::Text flag).

    The caseSensitivity parameter specifies whether keys are case sensitive or not. This makes a difference when looking up values using QSettings. The default is case sensitive.

    By default, if you use one of the constructors that work in terms of an organization name and an application name, the file system locations used are the same as for IniFormat. Use setPath() to specify other locations.

    Example:

    1. bool readXmlFile(QIODevice &device, QSettings::SettingsMap &map);
    2. bool writeXmlFile(QIODevice &device, const QSettings::SettingsMap &map);
    3.  
    4. int main(int argc, char *argv[])
    5.  {
    6.     const QSettings::Format XmlFormat =
    7.             QSettings::registerFormat("xml", readXmlFile, writeXmlFile);
    8.  
    9.     QSettings settings(XmlFormat, QSettings::UserScope, "MySoft",
    10.                        "Star Runner");
    11.  
    12.     ...
    13. }

    See also setPath().

    Look up this member in the source code.

    Toggle details void QSettings

    removeremove ( const QString &key ) ( const QString &key )

    Removes the setting key and any sub-settings of key.

    Example:

    1.                     QSettings settings;
    2. settings.setValue("ape");
    3. settings.setValue("monkey", 1);
    4. settings.setValue("monkey/sea", 2);
    5. settings.setValue("monkey/doe", 4);
    6.  
    7. settings.remove("monkey");
    8. QStringList keys = settings.allKeys();
    9. // keys: ["ape"]

    Be aware that if one of the fallback locations contains a setting with the same key, that setting will be visible after calling remove().

    If key is an empty string, all keys in the current group() are removed. For example:

    1.                     QSettings settings;
    2. settings.setValue("ape");
    3. settings.setValue("monkey", 1);
    4. settings.setValue("monkey/sea", 2);
    5. settings.setValue("monkey/doe", 4);
    6.  
    7. settings.beginGroup("monkey");
    8. settings.remove("");
    9. settings.endGroup();
    10.  
    11. QStringList keys = settings.allKeys();
    12. // keys: ["ape"]

    Note that the Windows registry and INI files use case-insensitive keys, whereas the Carbon Preferences API on Mac OS X uses case-sensitive keys. To avoid portability problems, see the Section and Key Syntax rules.

    See also setValue(), value(), and contains().

    Look up this member in the source code.

    Toggle details Scope QSettings

    scopescope () ()const

    Returns the scope used for storing the settings.

    See also format(), organizationName(), and applicationName().

    Look up this member in the source code.

    Toggle details void QSettings

    setArrayIndexsetArrayIndex ( int i ) ( int i )

    Sets the current array index to i. Calls to functions such as setValue(), value(), remove(), and contains() will operate on the array entry at that index.

    You must call beginReadArray() or beginWriteArray() before you can call this function.

    Look up this member in the source code.

    Toggle details void QSettings

    setDefaultFormatsetDefaultFormat ( Format format ) ( Format format ) [static]

    Sets the default file format to the given format, which is used for storing settings for the QSettings(QObject *) constructor.

    If no default format is set, QSettings::NativeFormat is used. See the documentation for the QSettings constructor you are using to see if that constructor will ignore this function.

    See also defaultFormat() and format().

    Look up this member in the source code.

    Toggle details void QSettings

    setFallbacksEnabledsetFallbacksEnabled ( bool b ) ( bool b )

    Sets whether fallbacks are enabled to b.

    By default, fallbacks are enabled.

    See also fallbacksEnabled().

    Look up this member in the source code.

    Toggle details void QSettings

    setIniCodecsetIniCodec ( QTextCodec *codec ) ( QTextCodec *codec )

    Sets the codec for accessing INI files (including .conf files on Unix) to codec. The codec is used for decoding any data that is read from the INI file, and for encoding any data that is written to the file. By default, no codec is used, and non-ASCII characters are encoded using standard INI escape sequences.

    Warning: The codec must be set immediately after creating the QSettings object, before accessing any data.

    See also iniCodec().

    Look up this member in the source code.

    Toggle details void QSettings

    setIniCodecsetIniCodec ( const char *codecName ) ( const char *codecName )

    This is an overloaded function.

    Sets the codec for accessing INI files (including .conf files on Unix) to the QTextCodec for the encoding specified by codecName. Common values for codecName include "ISO 8859-1", "UTF-8", and "UTF-16". If the encoding isn't recognized, nothing happens.

    See also QTextCodec::codecForName().

    Look up this member in the source code.

    Toggle details void QSettings

    setPathsetPath ( Format format , Scope scope , const QString &path ...) ( Format format , Scope scope , const QString &path ) [static]

    Sets the path used for storing settings for the given format and scope, to path. The format can be a custom format.

    The table below summarizes the default values:

    PlatformFormatScopePath
    Windows IniFormat UserScope %APPDATA%
    SystemScope %COMMON_APPDATA%
    Unix NativeFormat, IniFormat UserScope $HOME/.config
    SystemScope /etc/xdg
    Qt for Embedded Linux NativeFormat, IniFormat UserScope $HOME/Settings
    SystemScope /etc/xdg
    Mac OS X IniFormat UserScope $HOME/.config
    SystemScope /etc/xdg

    The default UserScope paths on Unix and Mac OS X ($HOME/.config or $HOME/Settings) can be overridden by the user by setting the XDG_CONFIG_HOME environment variable. The default SystemScope paths on Unix and Mac OS X (/etc/xdg) can be overridden when building the Qt library using the configure script's --sysconfdir flag (see QLibraryInfo for details).

    Setting the NativeFormat paths on Windows and Mac OS X has no effect.

    Warning: This function doesn't affect existing QSettings objects.

    See also registerFormat().

    Look up this member in the source code.

    Toggle details void QSettings

    setValuesetValue ( const QString &key , const QVariant &value ...) ( const QString &key , const QVariant &value )

    Sets the value of setting key to value. If the key already exists, the previous value is overwritten.

    Note that the Windows registry and INI files use case-insensitive keys, whereas the Carbon Preferences API on Mac OS X uses case-sensitive keys. To avoid portability problems, see the Section and Key Syntax rules.

    Example:

    1.                     QSettings settings;
    2. settings.setValue("interval", 30);
    3. settings.value("interval").toInt();     // returns 30
    4.  
    5. settings.setValue("interval", 6.55);
    6. settings.value("interval").toDouble();  // returns 6.55

    See also value(), remove(), and contains().

    Look up this member in the source code.

    Toggle details Status QSettings

    statusstatus () ()const

    Returns a status code indicating the first error that was met by QSettings, or QSettings::NoError if no error occurred.

    Be aware that QSettings delays performing some operations. For this reason, you might want to call sync() to ensure that the data stored in QSettings is written to disk before calling status().

    See also sync().

    Look up this member in the source code.

    Toggle details void QSettings

    syncsync () ()

    Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in the meantime by another application.

    This function is called automatically from QSettings's destructor and by the event loop at regular intervals, so you normally don't need to call it yourself.

    See also status().

    Look up this member in the source code.

    Toggle details QVariant QSettings

    valuevalue ( const QString &key , const QVariant &defaultValue=QVariant() ...) ( const QString &key , const QVariant &defaultValue=QVariant() )const

    Returns the value for setting key. If the setting doesn't exist, returns defaultValue.

    If no default value is specified, a default QVariant is returned.

    Note that the Windows registry and INI files use case-insensitive keys, whereas the Carbon Preferences API on Mac OS X uses case-sensitive keys. To avoid portability problems, see the Section and Key Syntax rules.

    Example:

    1.                     QSettings settings;
    2. settings.setValue("animal/snake", 58);
    3. settings.value("animal/snake", 1024).toInt();   // returns 58
    4. settings.value("animal/zebra", 1024).toInt();   // returns 1024
    5. settings.value("animal/zebra").toInt();         // returns 0

    See also setValue(), contains(), and remove().

    Look up this member in the source code.

    Toggle details void QSettings

    setSystemIniPathsetSystemIniPath ( const QString &dir ) ( const QString &dir ) [static] Obsolete function

    Use setPath() instead.

    For example, if you have code like

    1. setSystemIniPath(path);

    you can rewrite it as

    1. setPath(QSettings::NativeFormat, QSettings::SystemScope, path);
    2. setPath(QSettings::IniFormat, QSettings::SystemScope, path);

    Look up this member in the source code.

    Toggle details void QSettings

    setUserIniPathsetUserIniPath ( const QString &dir ) ( const QString &dir ) [static] Obsolete function

    Use setPath() instead.

    Look up this member in the source code.

    Toggle details QStringList QSettings

    entryListentryList ( const QString &key ) ( const QString &key )const Qt3-support

    Returns a list of all sub-keys of key.

    Use childKeys() instead.

    For example, if you have code like

    1. QSettings settings;
    2. QStringList keys = settings.entryList("cities");
    3. ...

    you can rewrite it as

    1.                     QSettings settings;
    2. settings.beginGroup("cities");
    3. QStringList keys = settings.childKeys();
    4. ...
    5. settings.endGroup();

    Look up this member in the source code.

    Toggle details void QSettings

    insertSearchPathinsertSearchPath ( System system , const QString &path ...) ( System system , const QString &path ) Qt3-support

    This function is implemented as a no-op. It is provided for source compatibility with Qt 3. The new QSettings class has no concept of "search path".

    Look up this member in the source code.

    Toggle details bool QSettings

    readBoolEntryreadBoolEntry ( const QString &key , bool defaultValue=false , bool *ok=0 ...) ( const QString &key , bool defaultValue=false , bool *ok=0 ) Qt3-support

    Returns the value for setting key converted to a bool. If the setting doesn't exist, returns defaultValue.

    If ok is not 0, *ok is set to true if the key exists, otherwise *ok is set to false.

    Use value() instead.

    For example, if you have code like

    1. bool ok;
    2. bool grid = settings.readBoolEntry("showGrid", true, &ok);

    you can rewrite it as

    1. bool ok = settings.contains("showGrid");
    2. bool grid = settings.value("showGrid", true).toBool();

    Look up this member in the source code.

    Toggle details double QSettings

    readDoubleEntryreadDoubleEntry ( const QString &key , double defaultValue=0 , bool *ok=0 ...) ( const QString &key , double defaultValue=0 , bool *ok=0 ) Qt3-support

    Returns the value for setting key converted to a double. If the setting doesn't exist, returns defaultValue.

    If ok is not 0, *ok is set to true if the key exists, otherwise *ok is set to false.

    Use value() instead.

    For example, if you have code like

    1. bool ok;
    2. double pi = settings.readDoubleEntry("pi", 3.141592, &ok);

    you can rewrite it as

    1. bool ok = settings.contains("pi");
    2. double pi = settings.value("pi", 3.141592).toDouble();

    Look up this member in the source code.

    Toggle details QString QSettings

    readEntryreadEntry ( const QString &key , const QString &defaultValue=QString() , bool *ok=0 ...) ( const QString &key , const QString &defaultValue=QString() , bool *ok=0 ) Qt3-support

    Returns the value for setting key converted to a QString. If the setting doesn't exist, returns defaultValue.

    If ok is not 0, *ok is set to true if the key exists, otherwise *ok is set to false.

    Use value() instead.

    For example, if you have code like

    1. bool ok;
    2. QString str = settings.readEntry("userName", "administrator", &ok);

    you can rewrite it as

    1. bool ok = settings.contains("userName");
    2. QString str = settings.value("userName", "administrator").toString();

    Look up this member in the source code.

    Toggle details QStringList QSettings

    readListEntryreadListEntry ( const QString &key , bool *ok=0 ...) ( const QString &key , bool *ok=0 ) Qt3-support

    Returns the value of setting key converted to a QStringList.

    If ok is not 0, *ok is set to true if the key exists, otherwise *ok is set to false.

    Use value() instead.

    For example, if you have code like

    1. bool ok;
    2. QStringList list = settings.readListEntry("recentFiles", &ok);

    you can rewrite it as

    1. bool ok = settings.contains("recentFiles");
    2. QStringList list = settings.value("recentFiles").toStringList();

    Look up this member in the source code.

    Toggle details QStringList QSettings

    readListEntryreadListEntry ( const QString &key , QChar separator , bool *ok=0 ...) ( const QString &key , QChar separator , bool *ok=0 ) Qt3-support

    Returns the value of setting key converted to a QStringList. separator is ignored.

    If ok is not 0, *ok is set to true if the key exists, otherwise *ok is set to false.

    Use value() instead.

    For example, if you have code like

    1. bool ok;
    2. QStringList list = settings.readListEntry("recentFiles", ":", &ok);

    you can rewrite it as

    1. bool ok = settings.contains("recentFiles");
    2. QStringList list = settings.value("recentFiles").toStringList();

    Look up this member in the source code.

    Toggle details int QSettings

    readNumEntryreadNumEntry ( const QString &key , int defaultValue=0 , bool *ok=0 ...) ( const QString &key , int defaultValue=0 , bool *ok=0 ) Qt3-support

    Returns the value for setting key converted to an int. If the setting doesn't exist, returns defaultValue.

    If ok is not 0, *ok is set to true if the key exists, otherwise *ok is set to false.

    Use value() instead.

    For example, if you have code like

    1. bool ok;
    2. int max = settings.readNumEntry("maxConnections", 30, &ok);

    you can rewrite it as

    1. bool ok = settings.contains("maxConnections");
    2. int max = settings.value("maxConnections", 30).toInt();

    Look up this member in the source code.

    Toggle details bool QSettings

    removeEntryremoveEntry ( const QString &key ) ( const QString &key ) Qt3-support

    Use remove() instead.

    Look up this member in the source code.

    Toggle details void QSettings

    removeSearchPathremoveSearchPath ( System system , const QString &path ...) ( System system , const QString &path ) Qt3-support

    This function is implemented as a no-op. It is provided for source compatibility with Qt 3. The new QSettings class has no concept of "search path".

    Look up this member in the source code.

    Toggle details void QSettings

    resetGroupresetGroup () () Qt3-support

    Sets the current group to be the empty string.

    Use endGroup() instead (possibly multiple times).

    For example, if you have code like

    1. QSettings settings;
    2. settings.beginGroup("mainWindow");
    3. settings.beginGroup("leftPanel");
    4. ...
    5. settings.resetGroup();

    you can rewrite it as

    1.                     QSettings settings;
    2. settings.beginGroup("mainWindow");
    3. settings.beginGroup("leftPanel");
    4. ...
    5. settings.endGroup();
    6. settings.endGroup();

    Look up this member in the source code.

    Toggle details void QSettings

    setPathsetPath ( const QString &organization , const QString &application , Scope scope=Global ...) ( const QString &organization , const QString &application , Scope scope=Global ) Qt3-support

    Specifies the organization, application, and scope to use by the QSettings object.

    Use the appropriate constructor instead, with QSettings::UserScope instead of QSettings::User and QSettings::SystemScope instead of QSettings::Global.

    For example, if you have code like

    1. QSettings settings;
    2. settings.setPath("twikimaster.com", "Kanooth", QSettings::Global);

    you can rewrite it as

    1.                     QSettings settings(QSettings::SystemScope, "twikimaster.com", "Kanooth");

    Look up this member in the source code.

    Toggle details QStringList QSettings

    subkeyListsubkeyList ( const QString &key ) ( const QString &key )const Qt3-support

    Returns a list of all sub-keys of key.

    Use childGroups() instead.

    For example, if you have code like

    1. QSettings settings;
    2. QStringList groups = settings.entryList("cities");
    3. ...

    you can rewrite it as

    1.                     QSettings settings;
    2. settings.beginGroup("cities");
    3. QStringList groups = settings.childKeys();
    4. ...
    5. settings.endGroup();

    Look up this member in the source code.

    Toggle details bool QSettings

    writeEntrywriteEntry ( const QString &key , bool value ) ( const QString &key , bool value ) Qt3-support

    Sets the value of setting key to value.

    Use setValue() instead.

    Look up this member in the source code.

    Toggle details bool QSettings

    writeEntrywriteEntry ( const QString &key , double value ) ( const QString &key , double value ) Qt3-support

    This is an overloaded function.

    Look up this member in the source code.

    Toggle details bool QSettings

    writeEntrywriteEntry ( const QString &key , int value ) ( const QString &key , int value ) Qt3-support

    This is an overloaded function.

    Look up this member in the source code.

    Toggle details bool QSettings

    writeEntrywriteEntry ( const QString &key , const char *value ...) ( const QString &key , const char *value ) Qt3-support

    This is an overloaded function.

    Look up this member in the source code.

    Toggle details bool QSettings

    writeEntrywriteEntry ( const QString &key , const QString &value ...) ( const QString &key , const QString &value ) Qt3-support

    This is an overloaded function.

    Look up this member in the source code.

    Toggle details bool QSettings

    writeEntrywriteEntry ( const QString &key , const QStringList &value ...) ( const QString &key , const QStringList &value ) Qt3-support

    This is an overloaded function.

    Look up this member in the source code.

    Toggle details bool QSettings

    writeEntrywriteEntry ( const QString &key , const QStringList &value , QChar separator ...) ( const QString &key , const QStringList &value , QChar separator ) Qt3-support

    This is an overloaded function.

    Use setValue(key, value) instead. You don't need separator.

    Look up this member in the source code.

    Notes provided by the Qt Community
    Informative
    • 5

    Votes: 6

    Coverage: Qt library 4.7, 4.8, 5.0

    Picture of Volker Volker

    Ant Farmer
    35 notes

    Nokia Certified Qt Developer

    File is created on destruction of QSettings object

    Class QSettings creates a new .ini file if it not yet present. The file is neither created on creation of the QSettings object nor on writing the first value, but on destruction of the QSettings object.

    Use sync() to force the QSettings object to flush the changes to disk.

    [Revisions]

    Informative
    • 5

    Votes: 4

    Coverage: Qt library 4.7, 4.8, 5.0

    Picture of Andre Andre

    Robot Herder
    30 notes

    Nokia Certified Qt Developer

    Correction to sample for beginWriteArray

    There is a small error in the beginWriteArray documentation. The correct version is:

    1. struct Login  {
    2.     QString userName;
    3.     QString password;
    4. };
    5. QList<Login> logins;
    6. ...
    7.  
    8. QSettings settings;
    9. settings.beginWriteArray ("logins");
    10. for  ( i = 0; i < logins.size (); ++i)  {
    11.     settings.setArrayIndex (i);
    12.     settings.setValue ("userName", logins.at (i).userName);
    13.     settings.setValue ("password", logins.at (i).password);
    14. }
    15. settings.endArray ();

    The difference is that in the writing loop, the logins variable is used instead of an undeclared variable called list on lines 12 and 13.

    Thanks to alfah [developer.qt.nokia.com] for noticing [developer.qt.nokia.com] .

    [Revisions]