<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html><head><title>QSettings Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm } a:link { color: #004faf; text-decoration: none } a:visited { color: #672967; text-decoration: none } td.postheader { font-family: sans-serif } tr.address { font-family: sans-serif } body { background: #ffffff; color: black; } </style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">  </td><td class="postheader" valign="center"><a href="../pyqt4ref.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QSettings Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1><p>The QSettings class provides persistent platform-independent application settings. <a href="#details">More...</a></p> <p>Inherits <a href="qobject.html">QObject</a>.</p><h3>Types</h3><ul><li><div class="fn" />enum <b><a href="qsettings.html#Format-enum">Format</a></b> { NativeFormat, IniFormat, InvalidFormat }</li><li><div class="fn" />enum <b><a href="qsettings.html#Scope-enum">Scope</a></b> { UserScope, SystemScope }</li><li><div class="fn" />enum <b><a href="qsettings.html#Status-enum">Status</a></b> { NoError, AccessError, FormatError }</li></ul><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qsettings.html#QSettings">__init__</a></b> (<i>self</i>, QString <i>organization</i>, QString <i>application</i> = QString(), QObject <i>parent</i> = None)</li><li><div class="fn" /><b><a href="qsettings.html#QSettings-2">__init__</a></b> (<i>self</i>, Scope <i>scope</i>, QString <i>organization</i>, QString <i>application</i> = QString(), QObject <i>parent</i> = None)</li><li><div class="fn" /><b><a href="qsettings.html#QSettings-3">__init__</a></b> (<i>self</i>, Format <i>format</i>, Scope <i>scope</i>, QString <i>organization</i>, QString <i>application</i> = QString(), QObject <i>parent</i> = None)</li><li><div class="fn" /><b><a href="qsettings.html#QSettings-4">__init__</a></b> (<i>self</i>, QString <i>fileName</i>, Format <i>format</i>, QObject <i>parent</i> = None)</li><li><div class="fn" /><b><a href="qsettings.html#QSettings-5">__init__</a></b> (<i>self</i>, QObject <i>parent</i> = None)</li><li><div class="fn" />QStringList <b><a href="qsettings.html#allKeys">allKeys</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsettings.html#applicationName">applicationName</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsettings.html#beginGroup">beginGroup</a></b> (<i>self</i>, QString <i>prefix</i>)</li><li><div class="fn" />int <b><a href="qsettings.html#beginReadArray">beginReadArray</a></b> (<i>self</i>, QString <i>prefix</i>)</li><li><div class="fn" /><b><a href="qsettings.html#beginWriteArray">beginWriteArray</a></b> (<i>self</i>, QString <i>prefix</i>, int <i>size</i> = -1)</li><li><div class="fn" />QStringList <b><a href="qsettings.html#childGroups">childGroups</a></b> (<i>self</i>)</li><li><div class="fn" />QStringList <b><a href="qsettings.html#childKeys">childKeys</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsettings.html#clear">clear</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsettings.html#contains">contains</a></b> (<i>self</i>, QString <i>key</i>)</li><li><div class="fn" /><b><a href="qsettings.html#endArray">endArray</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsettings.html#endGroup">endGroup</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsettings.html#event">event</a></b> (<i>self</i>, QEvent <i>event</i>)</li><li><div class="fn" />bool <b><a href="qsettings.html#fallbacksEnabled">fallbacksEnabled</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsettings.html#fileName">fileName</a></b> (<i>self</i>)</li><li><div class="fn" />Format <b><a href="qsettings.html#format">format</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsettings.html#group">group</a></b> (<i>self</i>)</li><li><div class="fn" />QTextCodec <b><a href="qsettings.html#iniCodec">iniCodec</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qsettings.html#isWritable">isWritable</a></b> (<i>self</i>)</li><li><div class="fn" />QString <b><a href="qsettings.html#organizationName">organizationName</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsettings.html#remove">remove</a></b> (<i>self</i>, QString <i>key</i>)</li><li><div class="fn" />Scope <b><a href="qsettings.html#scope">scope</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsettings.html#setArrayIndex">setArrayIndex</a></b> (<i>self</i>, int <i>i</i>)</li><li><div class="fn" /><b><a href="qsettings.html#setFallbacksEnabled">setFallbacksEnabled</a></b> (<i>self</i>, bool <i>b</i>)</li><li><div class="fn" /><b><a href="qsettings.html#setIniCodec">setIniCodec</a></b> (<i>self</i>, QTextCodec <i>codec</i>)</li><li><div class="fn" /><b><a href="qsettings.html#setIniCodec-2">setIniCodec</a></b> (<i>self</i>, str <i>codecName</i>)</li><li><div class="fn" /><b><a href="qsettings.html#setValue">setValue</a></b> (<i>self</i>, QString <i>key</i>, QVariant <i>value</i>)</li><li><div class="fn" />Status <b><a href="qsettings.html#status">status</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsettings.html#sync">sync</a></b> (<i>self</i>)</li><li><div class="fn" />object <b><a href="qsettings.html#value">value</a></b> (<i>self</i>, QString <i>key</i>, QVariant <i>defaultValue</i> = QVariant(), object <i>type</i> = None)</li></ul><h3>Static Methods</h3><ul><li><div class="fn" />Format <b><a href="qsettings.html#defaultFormat">defaultFormat</a></b> ()</li><li><div class="fn" /><b><a href="qsettings.html#setDefaultFormat">setDefaultFormat</a></b> (Format <i>format</i>)</li><li><div class="fn" /><b><a href="qsettings.html#setPath">setPath</a></b> (Format <i>format</i>, Scope <i>scope</i>, QString <i>path</i>)</li><li><div class="fn" /><b><a href="qsettings.html#setSystemIniPath">setSystemIniPath</a></b> (QString <i>dir</i>)</li><li><div class="fn" /><b><a href="qsettings.html#setUserIniPath">setUserIniPath</a></b> (QString <i>dir</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QSettings class provides persistent platform-independent application settings.</p> <p>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.</p> <p>QSettings is an abstraction around these technologies, enabling you to save and restore application settings in a portable manner. It also supports <a href="qsettings.html#registerFormat">custom storage formats</a>.</p> <p>QSettings's API is based on <a href="qvariant.html">QVariant</a>, allowing you to save most value-based types, such as <a href="qstring.html">QString</a>, <a href="qrect.html">QRect</a>, and <a href="qimage.html">QImage</a>, with the minimum of effort.</p> <p>If all you need is a non-persistent memory-based structure, consider using <a href="qmap.html">QMap</a><<a href="qstring.html">QString</a>, <a href="qvariant.html">QVariant</a>> instead.</p> <a id="basic-usage" name="basic-usage" /> <h3>Basic Usage</h3> <p>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:</p> <pre class="highlightedCode brush: cpp"> QSettings settings("MySoft", "Star Runner"); </pre> <p>QSettings objects can be created either on the stack or on the heap (i.e. using <tt>new</tt>). Constructing and destroying a QSettings object is very fast.</p> <p>If you use QSettings from many places in your application, you might want to specify the organization name and the application name using <a href="qcoreapplication.html#organizationName-prop">QCoreApplication.setOrganizationName</a>() and <a href="qcoreapplication.html#applicationName-prop">QCoreApplication.setApplicationName</a>(), and then use the default QSettings constructor:</p> <pre class="highlightedCode brush: cpp"> QCoreApplication.setOrganizationName("MySoft"); QCoreApplication.setOrganizationDomain("mysoft.com"); QCoreApplication.setApplicationName("Star Runner"); ... QSettings settings; </pre> <p>(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 <a href="#platform-specific-notes">Platform-Specific Notes</a> below for details.)</p> <p>QSettings stores settings. Each setting consists of a <a href="qstring.html">QString</a> that specifies the setting's name (the <i>key</i>) and a <a href="qvariant.html">QVariant</a> that stores the data associated with the key. To write a setting, use <a href="qsettings.html#setValue">setValue</a>(). For example:</p> <pre class="highlightedCode brush: cpp"> settings.setValue("editor/wrapMargin", 68); </pre> <p>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 <a href="qsettings.html#sync">sync</a>() to commit your changes.)</p> <p>You can get a setting's value back using <a href="qsettings.html#value">value</a>():</p> <pre class="highlightedCode brush: cpp"> int margin = settings.value("editor/wrapMargin").toInt(); </pre> <p>If there is no setting with the specified name, QSettings returns a null <a href="qvariant.html">QVariant</a> (which can be converted to the integer 0). You can specify another default value by passing a second argument to <a href="qsettings.html#value">value</a>():</p> <pre class="highlightedCode brush: cpp"> int margin = settings.value("editor/wrapMargin", 80).toInt(); </pre> <p>To test whether a given key exists, call <a href="qsettings.html#contains">contains</a>(). To remove the setting associated with a key, call <a href="qsettings.html#remove">remove</a>(). To obtain the list of all keys, call <a href="qsettings.html#allKeys">allKeys</a>(). To remove all keys, call <a href="qsettings.html#clear">clear</a>().</p> <a id="qvariant-and-gui-types" name="qvariant-and-gui-types" /> <h3>QVariant and GUI Types</h3> <p>Because <a href="qvariant.html">QVariant</a> is part of the <a href="qtcore.html">QtCore</a> library, it cannot provide conversion functions to data types such as <a href="qcolor.html">QColor</a>, <a href="qimage.html">QImage</a>, and <a href="qpixmap.html">QPixmap</a>, which are part of <a href="qtgui.html">QtGui</a>. In other words, there is no <tt>toColor()</tt>, <tt>toImage()</tt>, or <tt>toPixmap()</tt> functions in <a href="qvariant.html">QVariant</a>.</p> <p>Instead, you can use the <a href="qvariant.html#value">QVariant.value</a>() or the <a href="qvariant.html#qVariantValue">qVariantValue</a>() template function. For example:</p> <pre class="highlightedCode brush: cpp"> QSettings settings("MySoft", "Star Runner"); QColor color = settings.value("DataPump/bgcolor").value<QColor>(); </pre> <p>The inverse conversion (e.g., from <a href="qcolor.html">QColor</a> to <a href="qvariant.html">QVariant</a>) is automatic for all data types supported by <a href="qvariant.html">QVariant</a>, including GUI-related types:</p> <pre class="highlightedCode brush: cpp"> QSettings settings("MySoft", "Star Runner"); QColor color = palette().background().color(); settings.setValue("DataPump/bgcolor", color); </pre> <p>Custom types registered using <a href="qmetatype.html#qRegisterMetaType">qRegisterMetaType</a>() and <a href="qmetatype.html#qRegisterMetaTypeStreamOperators">qRegisterMetaTypeStreamOperators</a>() can be stored using QSettings.</p> <a id="section-and-key-syntax" name="section-and-key-syntax" /> <h3>Section and Key Syntax</h3> <p>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:</p> <ol class="1"> <li>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.</li> <li>Avoid key names that are identical except for the case. For example, if you have a key called "<a href="designer-to-know.html#mainwindow">MainWindow</a>", don't try to save another key as "mainwindow".</li> <li>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.</li> </ol> <p>You can form hierarchical keys using the '/' character as a separator, similar to Unix file paths. For example:</p> <pre class="highlightedCode brush: cpp"> settings.setValue("mainwindow/size", win->size()); settings.setValue("mainwindow/fullScreen", win->isFullScreen()); settings.setValue("outputpanel/visible", panel->isVisible()); </pre> <p>If you want to save or restore many settings with the same prefix, you can specify the prefix using <a href="qsettings.html#beginGroup">beginGroup</a>() and call <a href="qsettings.html#endGroup">endGroup</a>() at the end. Here's the same example again, but this time using the group mechanism:</p> <pre class="highlightedCode brush: cpp"> settings.beginGroup("mainwindow"); settings.setValue("size", win->size()); settings.setValue("fullScreen", win->isFullScreen()); settings.endGroup(); settings.beginGroup("outputpanel"); settings.setValue("visible", panel->isVisible()); settings.endGroup(); </pre> <p>If a group is set using <a href="qsettings.html#beginGroup">beginGroup</a>(), the behavior of most functions changes consequently. Groups can be set recursively.</p> <p>In addition to groups, QSettings also supports an "array" concept. See <a href="qsettings.html#beginReadArray">beginReadArray</a>() and <a href="qsettings.html#beginWriteArray">beginWriteArray</a>() for details.</p> <a id="fallback-mechanism" name="fallback-mechanism" /> <h3>Fallback Mechanism</h3> <p>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:</p> <ol class="1"> <li>a user-specific location for the Star Runner application</li> <li>a user-specific location for all applications by MySoft</li> <li>a system-wide location for the Star Runner application</li> <li>a system-wide location for all applications by MySoft</li> </ol> <p>(See <a href="#platform-specific-notes">Platform-Specific Notes</a> below for information on what these locations are on the different platforms supported by Qt.)</p> <p>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).</p> <p>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 <a href="qsettings.html#Scope-enum">QSettings.SystemScope</a> (as opposed to <a href="qsettings.html#Scope-enum">QSettings.UserScope</a>, the default).</p> <p>Let's see with an example:</p> <pre class="highlightedCode brush: cpp"> QSettings obj1("MySoft", "Star Runner"); QSettings obj2("MySoft"); QSettings obj3(QSettings.SystemScope, "MySoft", "Star Runner"); QSettings obj4(QSettings.SystemScope, "MySoft"); </pre> <p>The table below summarizes which QSettings objects access which location. "<b>X</b>" 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.</p> <table class="generic"> <thead> <tr class="qt-style topAlign"> <th>Locations</th> <th><tt>obj1</tt></th> <th><tt>obj2</tt></th> <th><tt>obj3</tt></th> <th><tt>obj4</tt></th> </tr> </thead> <tr class="odd topAlign"> <td> <p>1. User, Application</p> </td> <td> <p><b>X</b></p> </td> <td /> <td /> <td /> </tr> <tr class="even topAlign"> <td> <p>2. User, Organization</p> </td> <td> <p>o</p> </td> <td> <p><b>X</b></p> </td> <td /> <td /> </tr> <tr class="odd topAlign"> <td> <p>3. System, Application</p> </td> <td> <p>o</p> </td> <td /> <td> <p><b>X</b></p> </td> <td /> </tr> <tr class="even topAlign"> <td> <p>4. System, Organization</p> </td> <td> <p>o</p> </td> <td> <p>o</p> </td> <td> <p>o</p> </td> <td> <p><b>X</b></p> </td> </tr> </table> <p>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.</p> <p>If you want to use INI files on all platforms instead of the native API, you can pass <a href="qsettings.html#Format-enum">QSettings.IniFormat</a> as the first argument to the QSettings constructor, followed by the scope, the organization name, and the application name:</p> <pre class="highlightedCode brush: cpp"> QSettings settings(QSettings.IniFormat, QSettings.UserScope, "MySoft", "Star Runner"); </pre> <p>The <a href="tools-settingseditor.html">Settings Editor</a> example lets you experiment with different settings location and with fallbacks turned on or off.</p> <a id="restoring-the-state-of-a-gui-application" name="restoring-the-state-of-a-gui-application" /> <h3>Restoring the State of a GUI Application</h3> <p>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.</p> <pre class="highlightedCode brush: cpp"> void MainWindow.writeSettings() { QSettings settings("Moose Soft", "Clipper"); settings.beginGroup("MainWindow"); settings.setValue("size", size()); settings.setValue("pos", pos()); settings.endGroup(); } void MainWindow.readSettings() { QSettings settings("Moose Soft", "Clipper"); settings.beginGroup("MainWindow"); resize(settings.value("size", QSize(400, 400)).toSize()); move(settings.value("pos", QPoint(200, 200)).toPoint()); settings.endGroup(); } </pre> <p>See <a href="application-windows.html#window-geometry">Window Geometry</a> for a discussion on why it is better to call <a href="qwidget.html#size-prop">QWidget.resize</a>() and <a href="qwidget.html#pos-prop">QWidget.move</a>() rather than <a href="qwidget.html#geometry-prop">QWidget.setGeometry</a>() to restore a window's geometry.</p> <p>The <tt>readSettings()</tt> and <tt>writeSettings()</tt> functions must be called from the main window's constructor and close event handler as follows:</p> <pre class="highlightedCode brush: cpp"> MainWindow.MainWindow() { ... readSettings(); } void MainWindow.closeEvent(QCloseEvent *event) { if (userReallyWantsToQuit()) { writeSettings(); event->accept(); } else { event->ignore(); } } </pre> <p>See the <a href="mainwindows-application.html">Application</a> example for a self-contained example that uses QSettings.</p> <a id="accessing-settings-from-multiple-threads-or-processes-simultaneously" name="accessing-settings-from-multiple-threads-or-processes-simultaneously" /> <h3>Accessing Settings from Multiple Threads or Processes Simultaneously</h3> <p>QSettings is <a href="threads-reentrancy.html#reentrant">reentrant</a>. 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.</p> <p>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 <a href="qsettings.html#sync">sync</a>() imports changes made by other processes (in addition to writing the changes from this QSettings).</p> <a id="platform-specific-notes" name="platform-specific-notes" /> <h3>Platform-Specific Notes</h3> <a id="locations-where-application-settings-are-stored" name="locations-where-application-settings-are-stored" /> <h4>Locations Where Application Settings Are Stored</h4> <p>As mentioned in the <a href="#fallback-mechanism">Fallback Mechanism</a> 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.</p> <p>On Unix systems, if the file format is <a href="qsettings.html#Format-enum">NativeFormat</a>, the following files are used by default:</p> <ol class="1"> <li><tt>$HOME/.config/MySoft/Star Runner.conf</tt> (Qt for Embedded Linux: <tt>$HOME/Settings/MySoft/Star Runner.conf</tt>)</li> <li><tt>$HOME/.config/MySoft.conf</tt> (Qt for Embedded Linux: <tt>$HOME/Settings/MySoft.conf</tt>)</li> <li><tt>/etc/xdg/MySoft/Star Runner.conf</tt></li> <li><tt>/etc/xdg/MySoft.conf</tt></li> </ol> <p>On Mac OS X versions 10.2 and 10.3, these files are used by default:</p> <ol class="1"> <li><tt>$HOME/Library/Preferences/com.MySoft.Star Runner.plist</tt></li> <li><tt>$HOME/Library/Preferences/com.MySoft.plist</tt></li> <li><tt>/Library/Preferences/com.MySoft.Star Runner.plist</tt></li> <li><tt>/Library/Preferences/com.MySoft.plist</tt></li> </ol> <p>On Windows, <a href="qsettings.html#Format-enum">NativeFormat</a> settings are stored in the following registry paths:</p> <ol class="1"> <li><tt>HKEY_CURRENT_USER\Software\MySoft\Star Runner</tt></li> <li><tt>HKEY_CURRENT_USER\Software\MySoft</tt></li> <li><tt>HKEY_LOCAL_MACHINE\Software\MySoft\Star Runner</tt></li> <li><tt>HKEY_LOCAL_MACHINE\Software\MySoft</tt></li> </ol> <p><b>Note:</b> On Windows, for 32-bit programs running in WOW64 mode, settings are stored in the following registry path: <tt>HKEY_LOCAL_MACHINE\Software\WOW6432node</tt>.</p> <p>If the file format is <a href="qsettings.html#Format-enum">IniFormat</a>, the following files are used on Unix and Mac OS X:</p> <ol class="1"> <li><tt>$HOME/.config/MySoft/Star Runner.ini</tt> (Qt for Embedded Linux: <tt>$HOME/Settings/MySoft/Star Runner.ini</tt>)</li> <li><tt>$HOME/.config/MySoft.ini</tt> (Qt for Embedded Linux: <tt>$HOME/Settings/MySoft.ini</tt>)</li> <li><tt>/etc/xdg/MySoft/Star Runner.ini</tt></li> <li><tt>/etc/xdg/MySoft.ini</tt></li> </ol> <p>On Windows, the following files are used:</p> <ol class="1"> <li><tt>%APPDATA%\MySoft\Star Runner.ini</tt></li> <li><tt>%APPDATA%\MySoft.ini</tt></li> <li><tt>%COMMON_APPDATA%\MySoft\Star Runner.ini</tt></li> <li><tt>%COMMON_APPDATA%\MySoft.ini</tt></li> </ol> <p>The <tt>%APPDATA%</tt> path is usually <tt>C:\Documents and Settings\<i>User Name</i>\Application Data</tt>; the <tt>%COMMON_APPDATA%</tt> path is usually <tt>C:\Documents and Settings\All Users\Application Data</tt>.</p> <p>The paths for the <tt>.ini</tt> and <tt>.conf</tt> files can be changed using <a href="qsettings.html#setPath">setPath</a>(). On Unix and Mac OS X, the user can override them by by setting the <tt>XDG_CONFIG_HOME</tt> environment variable; see <a href="qsettings.html#setPath">setPath</a>() for details.</p> <a id="accessing-ini-and-plist-files-directly" name="accessing-ini-and-plist-files-directly" /> <h4>Accessing INI and .plist Files Directly</h4> <p>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 <a href="qsettings.html#Format-enum">QSettings.IniFormat</a> as second argument. For example:</p> <pre class="highlightedCode brush: cpp"> QSettings settings("/home/petra/misc/myapp.ini", QSettings.IniFormat); </pre> <p>You can then use the QSettings object to read and write settings in the file.</p> <p>On Mac OS X, you can access XML-based <tt>.plist</tt> files by passing <a href="qsettings.html#Format-enum">QSettings.NativeFormat</a> as second argument. For example:</p> <pre class="highlightedCode brush: cpp"> QSettings settings("/Users/petra/misc/myapp.plist", QSettings.NativeFormat); </pre> <a id="accessing-the-windows-registry-directly" name="accessing-the-windows-registry-directly" /> <h4>Accessing the Windows Registry Directly</h4> <p>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 <a href="qsettings.html#Format-enum">QSettings.NativeFormat</a>.</p> <p>For example:</p> <pre class="highlightedCode brush: cpp"> QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Office", QSettings.NativeFormat); </pre> <p>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:</p> <pre class="highlightedCode brush: cpp"> settings.setValue("11.0/Outlook/Security/DontTrustInstalledFiles", 0); </pre> <p>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.</p> <a id="accessing-common-registry-settings-on-windows" name="accessing-common-registry-settings-on-windows" /> <h4>Accessing Common Registry Settings on Windows</h4> <p>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:</p> <pre class="highlightedCode brush: cpp"> settings.setValue("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy", "Milkyway"); settings.setValue("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy\\Sun", "OurStar"); settings.value("HKEY_CURRENT_USER\\MySoft\\Star Runner\\Galaxy\\Default"); <span class="comment">// returns "Milkyway"</span> </pre> <p>On other platforms than Windows, "Default" and "." would be treated as regular subkeys.</p> <a id="platform-limitations" name="platform-limitations" /> <h4>Platform Limitations</h4> <p>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:</p> <ul> <li>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 <a href="qsettings.html#Format-enum">IniFormat</a> instead of the <a href="qsettings.html#Format-enum">NativeFormat</a>.</li> <li>On Mac OS X, <a href="qsettings.html#allKeys">allKeys</a>() will return some extra keys for global settings that apply to all applications. These keys can be read using <a href="qsettings.html#value">value</a>() but cannot be changed, only shadowed. Calling setFallbacksEnabled(false) will hide these global settings.</li> <li>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 <a href="qcoreapplication.html#organizationDomain-prop">QCoreApplication.setOrganizationDomain</a>(), <a href="qcoreapplication.html#organizationName-prop">QCoreApplication.setOrganizationName</a>(), and <a href="qcoreapplication.html#applicationName-prop">QCoreApplication.setApplicationName</a>() in your <tt>main()</tt> function and then use the default QSettings constructor. Another solution is to use preprocessor directives, for example: <pre class="highlightedCode brush: cpp"> #ifdef Q_WS_MAC QSettings settings("grenoullelogique.fr", "Squash"); #else QSettings settings("Grenoulle Logique", "Squash"); #endif </pre></li> <li>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 <tt>.plist</tt> files.</li> </ul> <hr /><h2>Type Documentation</h2><h3 class="fn"><a name="Format-enum" />QSettings.Format</h3><p>This enum type specifies the storage format used by <a href="qsettings.html">QSettings</a>.</p> <table class="valuelist"> <tr class="odd"> <td /> </tr> <tr> <th class="tblConst">Constant</th> <th class="tblval">Value</th> <th class="tbldscr">Description</th> </tr> <tr> <td class="topAlign"><tt>QSettings.NativeFormat</tt></td> <td class=" topAlign"><tt>0</tt></td> <td class="topAlign">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.</td> </tr> <tr> <td class="topAlign"><tt>QSettings.IniFormat</tt></td> <td class=" topAlign"><tt>1</tt></td> <td class="topAlign">Store the settings in INI files.</td> </tr> <tr> <td class="topAlign"><tt>QSettings.InvalidFormat</tt></td> <td class=" topAlign"><tt>16</tt></td> <td class="topAlign">Special value returned by <a href="qsettings.html#registerFormat">registerFormat</a>().</td> </tr> </table> <p>On Unix, NativeFormat and IniFormat mean the same thing, except that the file extension is different (<tt>.conf</tt> for NativeFormat, <tt>.ini</tt> for IniFormat).</p> <p>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:</p> <ul> <li>If you store types that <a href="qvariant.html">QVariant</a> can't convert to <a href="qstring.html">QString</a> (e.g., <a href="qpoint.html">QPoint</a>, <a href="qrect.html">QRect</a>, and <a href="qsize.html">QSize</a>), Qt uses an <tt>@</tt>-based syntax to encode the type. For example: <pre class="highlightedCode brush: cpp"> pos = @Point(100 100) </pre> <p>To minimize compatibility issues, any <tt>@</tt> that doesn't appear at the first position in the value or that isn't followed by a Qt type (<tt>Point</tt>, <tt>Rect</tt>, <tt>Size</tt>, etc.) is treated as a normal character.</p> </li> <li>Although backslash is a special character in INI files, most Windows applications don't escape backslashes (<tt>\</tt>) in file paths: <pre class="highlightedCode brush: cpp"> windir = C:\Windows </pre> <p><a href="qsettings.html">QSettings</a> always treats backslash as a special character and provides no API for reading or writing such entries.</p> </li> <li>The INI file format has severe restrictions on the syntax of a key. Qt works around this by using <tt>%</tt> 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, <i>not</i> in the "General" section.</li> <li>Following the philosophy that we should be liberal in what we accept and conservative in what we generate, <a href="qsettings.html">QSettings</a> 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 <a href="qsettings.html#setIniCodec">setIniCodec</a>().</li> </ul> <p>See also <a href="qsettings.html#registerFormat">registerFormat</a>() and <a href="qsettings.html#setPath">setPath</a>().</p> <h3 class="fn"><a name="Scope-enum" />QSettings.Scope</h3><p>This enum specifies whether settings are user-specific or shared by all users of the same system.</p> <table class="valuelist"> <tr class="odd"> <td /> </tr> <tr> <th class="tblConst">Constant</th> <th class="tblval">Value</th> <th class="tbldscr">Description</th> </tr> <tr> <td class="topAlign"><tt>QSettings.UserScope</tt></td> <td class=" topAlign"><tt>0</tt></td> <td class="topAlign">Store settings in a location specific to the current user (e.g., in the user's home directory).</td> </tr> <tr> <td class="topAlign"><tt>QSettings.SystemScope</tt></td> <td class=" topAlign"><tt>1</tt></td> <td class="topAlign">Store settings in a global location, so that all users on the same machine access the same set of settings.</td> </tr> </table> <p>See also <a href="qsettings.html#setPath">setPath</a>().</p> <h3 class="fn"><a name="Status-enum" />QSettings.Status</h3><p>The following status values are possible:</p> <table class="valuelist"> <tr class="odd"> <td /> </tr> <tr> <th class="tblConst">Constant</th> <th class="tblval">Value</th> <th class="tbldscr">Description</th> </tr> <tr> <td class="topAlign"><tt>QSettings.NoError</tt></td> <td class=" topAlign"><tt>0</tt></td> <td class="topAlign">No error occurred.</td> </tr> <tr> <td class="topAlign"><tt>QSettings.AccessError</tt></td> <td class=" topAlign"><tt>1</tt></td> <td class="topAlign">An access error occurred (e.g. trying to write to a read-only file).</td> </tr> <tr> <td class="topAlign"><tt>QSettings.FormatError</tt></td> <td class=" topAlign"><tt>2</tt></td> <td class="topAlign">A format error occurred (e.g. loading a malformed INI file).</td> </tr> </table> <p>See also <a href="qsettings.html#status">status</a>().</p> <hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QSettings" />QSettings.__init__ (<i>self</i>, QString <i>organization</i>, QString <i>application</i> = QString(), <a href="qobject.html">QObject</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a <a href="qsettings.html">QSettings</a> object for accessing settings of the application called <i>application</i> from the organization called <i>organization</i>, and with parent <i>parent</i>.</p> <p>Example:</p> <pre class="highlightedCode brush: cpp"> QSettings settings("Moose Tech", "Facturo-Pro"); </pre> <p>The scope is set to <a href="qsettings.html#Scope-enum">QSettings.UserScope</a>, and the format is set to <a href="qsettings.html#Format-enum">QSettings.NativeFormat</a> (i.e. calling <a href="qsettings.html#setDefaultFormat">setDefaultFormat</a>() before calling this constructor has no effect).</p> <p>See also <a href="qsettings.html#setDefaultFormat">setDefaultFormat</a>() and <a href="qsettings.html#fallback-mechanism">Fallback Mechanism</a>.</p> <h3 class="fn"><a name="QSettings-2" />QSettings.__init__ (<i>self</i>, <a href="qsettings.html#Scope-enum">Scope</a> <i>scope</i>, QString <i>organization</i>, QString <i>application</i> = QString(), <a href="qobject.html">QObject</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a <a href="qsettings.html">QSettings</a> object for accessing settings of the application called <i>application</i> from the organization called <i>organization</i>, and with parent <i>parent</i>.</p> <p>If <i>scope</i> is <a href="qsettings.html#Scope-enum">QSettings.UserScope</a>, the <a href="qsettings.html">QSettings</a> object searches user-specific settings first, before it searches system-wide settings as a fallback. If <i>scope</i> is <a href="qsettings.html#Scope-enum">QSettings.SystemScope</a>, the <a href="qsettings.html">QSettings</a> object ignores user-specific settings and provides access to system-wide settings.</p> <p>The storage format is set to <a href="qsettings.html#Format-enum">QSettings.NativeFormat</a> (i.e. calling <a href="qsettings.html#setDefaultFormat">setDefaultFormat</a>() before calling this constructor has no effect).</p> <p>If no application name is given, the <a href="qsettings.html">QSettings</a> object will only access the organization-wide <a href="qsettings.html#fallback-mechanism">locations</a>.</p> <p>See also <a href="qsettings.html#setDefaultFormat">setDefaultFormat</a>().</p> <h3 class="fn"><a name="QSettings-3" />QSettings.__init__ (<i>self</i>, <a href="qsettings.html#Format-enum">Format</a> <i>format</i>, <a href="qsettings.html#Scope-enum">Scope</a> <i>scope</i>, QString <i>organization</i>, QString <i>application</i> = QString(), <a href="qobject.html">QObject</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a <a href="qsettings.html">QSettings</a> object for accessing settings of the application called <i>application</i> from the organization called <i>organization</i>, and with parent <i>parent</i>.</p> <p>If <i>scope</i> is <a href="qsettings.html#Scope-enum">QSettings.UserScope</a>, the <a href="qsettings.html">QSettings</a> object searches user-specific settings first, before it searches system-wide settings as a fallback. If <i>scope</i> is <a href="qsettings.html#Scope-enum">QSettings.SystemScope</a>, the <a href="qsettings.html">QSettings</a> object ignores user-specific settings and provides access to system-wide settings.</p> <p>If <i>format</i> is <a href="qsettings.html#Format-enum">QSettings.NativeFormat</a>, the native API is used for storing settings. If <i>format</i> is <a href="qsettings.html#Format-enum">QSettings.IniFormat</a>, the INI format is used.</p> <p>If no application name is given, the <a href="qsettings.html">QSettings</a> object will only access the organization-wide <a href="qsettings.html#fallback-mechanism">locations</a>.</p> <h3 class="fn"><a name="QSettings-4" />QSettings.__init__ (<i>self</i>, QString <i>fileName</i>, <a href="qsettings.html#Format-enum">Format</a> <i>format</i>, <a href="qobject.html">QObject</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a <a href="qsettings.html">QSettings</a> object for accessing the settings stored in the file called <i>fileName</i>, with parent <i>parent</i>. If the file doesn't already exist, it is created.</p> <p>If <i>format</i> is <a href="qsettings.html#Format-enum">QSettings.NativeFormat</a>, the meaning of <i>fileName</i> depends on the platform. On Unix, <i>fileName</i> is the name of an INI file. On Mac OS X, <i>fileName</i> is the name of a <tt>.plist</tt> file. On Windows, <i>fileName</i> is a path in the system registry.</p> <p>If <i>format</i> is <a href="qsettings.html#Format-enum">QSettings.IniFormat</a>, <i>fileName</i> is the name of an INI file.</p> <p><b>Warning:</b> This function is provided for convenience. It works well for accessing INI or <tt>.plist</tt> 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:</p> <ul> <li><a href="qsettings.html">QSettings</a> 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.)</li> <li>In INI files, <a href="qsettings.html">QSettings</a> uses the <tt>@</tt> character as a metacharacter in some contexts, to encode Qt-specific data types (e.g., <tt>@Rect</tt>), and might therefore misinterpret it when it occurs in pure INI files.</li> </ul> <p>See also <a href="qsettings.html#fileName">fileName</a>().</p> <h3 class="fn"><a name="QSettings-5" />QSettings.__init__ (<i>self</i>, <a href="qobject.html">QObject</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Constructs a <a href="qsettings.html">QSettings</a> object for accessing settings of the application and organization set previously with a call to <a href="qcoreapplication.html#organizationName-prop">QCoreApplication.setOrganizationName</a>(), <a href="qcoreapplication.html#organizationDomain-prop">QCoreApplication.setOrganizationDomain</a>(), and <a href="qcoreapplication.html#applicationName-prop">QCoreApplication.setApplicationName</a>().</p> <p>The scope is <a href="qsettings.html#Scope-enum">QSettings.UserScope</a> and the format is <a href="qsettings.html#defaultFormat">defaultFormat</a>() (<a href="qsettings.html#Format-enum">QSettings.NativeFormat</a> by default). Use <a href="qsettings.html#setDefaultFormat">setDefaultFormat</a>() before calling this constructor to change the default format used by this constructor.</p> <p>The code</p> <pre class="highlightedCode brush: cpp"> QSettings settings("Moose Soft", "Facturo-Pro"); </pre> <p>is equivalent to</p> <pre class="highlightedCode brush: cpp"> QCoreApplication.setOrganizationName("Moose Soft"); QCoreApplication.setApplicationName("Facturo-Pro"); QSettings settings; </pre> <p>If <a href="qcoreapplication.html#organizationName-prop">QCoreApplication.setOrganizationName</a>() and <a href="qcoreapplication.html#applicationName-prop">QCoreApplication.setApplicationName</a>() has not been previously called, the <a href="qsettings.html">QSettings</a> object will not be able to read or write any settings, and <a href="qsettings.html#status">status</a>() will return <a href="qsettings.html#Status-enum">AccessError</a>.</p> <p>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.</p> <p>See also <a href="qcoreapplication.html#organizationName-prop">QCoreApplication.setOrganizationName</a>(), <a href="qcoreapplication.html#organizationDomain-prop">QCoreApplication.setOrganizationDomain</a>(), <a href="qcoreapplication.html#applicationName-prop">QCoreApplication.setApplicationName</a>(), and <a href="qsettings.html#setDefaultFormat">setDefaultFormat</a>().</p> <h3 class="fn"><a name="allKeys" />QStringList QSettings.allKeys (<i>self</i>)</h3><p>Returns a list of all keys, including subkeys, that can be read using the <a href="qsettings.html">QSettings</a> object.</p> <p>Example:</p> <pre class="highlightedCode brush: cpp"> QSettings settings; settings.setValue("fridge/color", Qt.white); settings.setValue("fridge/size", QSize(32, 96)); settings.setValue("sofa", true); settings.setValue("tv", false); QStringList keys = settings.allKeys(); <span class="comment"> // keys: ["fridge/color", "fridge/size", "sofa", "tv"]</span> </pre> <p>If a group is set using <a href="qsettings.html#beginGroup">beginGroup</a>(), only the keys in the group are returned, without the group prefix:</p> <pre class="highlightedCode brush: cpp"> settings.beginGroup("fridge"); keys = settings.allKeys(); <span class="comment"> // keys: ["color", "size"]</span> </pre> <p>See also <a href="qsettings.html#childGroups">childGroups</a>() and <a href="qsettings.html#childKeys">childKeys</a>().</p> <h3 class="fn"><a name="applicationName" />QString QSettings.applicationName (<i>self</i>)</h3><p>Returns the application name used for storing the settings.</p> <p>This function was introduced in Qt 4.4.</p> <p>See also <a href="qcoreapplication.html#applicationName-prop">QCoreApplication.applicationName</a>(), <a href="qsettings.html#format">format</a>(), <a href="qsettings.html#scope">scope</a>(), and <a href="qsettings.html#organizationName">organizationName</a>().</p> <h3 class="fn"><a name="beginGroup" />QSettings.beginGroup (<i>self</i>, QString <i>prefix</i>)</h3><p>Appends <i>prefix</i> to the current group.</p> <p>The current group is automatically prepended to all keys specified to <a href="qsettings.html">QSettings</a>. In addition, query functions such as <a href="qsettings.html#childGroups">childGroups</a>(), <a href="qsettings.html#childKeys">childKeys</a>(), and <a href="qsettings.html#allKeys">allKeys</a>() are based on the group. By default, no group is set.</p> <p>Groups are useful to avoid typing in the same setting paths over and over. For example:</p> <pre class="highlightedCode brush: cpp"> settings.beginGroup("mainwindow"); settings.setValue("size", win->size()); settings.setValue("fullScreen", win->isFullScreen()); settings.endGroup(); settings.beginGroup("outputpanel"); settings.setValue("visible", panel->isVisible()); settings.endGroup(); </pre> <p>This will set the value of three settings:</p> <ul> <li><tt>mainwindow/size</tt></li> <li><tt>mainwindow/fullScreen</tt></li> <li><tt>outputpanel/visible</tt></li> </ul> <p>Call <a href="qsettings.html#endGroup">endGroup</a>() to reset the current group to what it was before the corresponding beginGroup() call. Groups can be nested.</p> <p>See also <a href="qsettings.html#endGroup">endGroup</a>() and <a href="qsettings.html#group">group</a>().</p> <h3 class="fn"><a name="beginReadArray" />int QSettings.beginReadArray (<i>self</i>, QString <i>prefix</i>)</h3><p>Adds <i>prefix</i> to the current group and starts reading from an array. Returns the size of the array.</p> <p>Example:</p> <pre class="highlightedCode brush: cpp"> struct Login { QString userName; QString password; }; QList<Login> logins; ... QSettings settings; int size = settings.beginReadArray("logins"); for (int i = 0; i < size; ++i) { settings.setArrayIndex(i); Login login; login.userName = settings.value("userName").toString(); login.password = settings.value("password").toString(); logins.append(login); } settings.endArray(); </pre> <p>Use <a href="qsettings.html#beginWriteArray">beginWriteArray</a>() to write the array in the first place.</p> <p>See also <a href="qsettings.html#beginWriteArray">beginWriteArray</a>(), <a href="qsettings.html#endArray">endArray</a>(), and <a href="qsettings.html#setArrayIndex">setArrayIndex</a>().</p> <h3 class="fn"><a name="beginWriteArray" />QSettings.beginWriteArray (<i>self</i>, QString <i>prefix</i>, int <i>size</i> = -1)</h3><p>Adds <i>prefix</i> to the current group and starts writing an array of size <i>size</i>. If <i>size</i> is -1 (the default), it is automatically determined based on the indexes of the entries written.</p> <p>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:</p> <pre class="highlightedCode brush: cpp"> struct Login { QString userName; QString password; }; QList<Login> logins; ... QSettings settings; settings.beginWriteArray("logins"); for (int i = 0; i < logins.size(); ++i) { settings.setArrayIndex(i); settings.setValue("userName", list.at(i).userName); settings.setValue("password", list.at(i).password); } settings.endArray(); </pre> <p>The generated keys will have the form</p> <ul> <li><tt>logins/size</tt></li> <li><tt>logins/1/userName</tt></li> <li><tt>logins/1/password</tt></li> <li><tt>logins/2/userName</tt></li> <li><tt>logins/2/password</tt></li> <li><tt>logins/3/userName</tt></li> <li><tt>logins/3/password</tt></li> <li>...</li> </ul> <p>To read back an array, use <a href="qsettings.html#beginReadArray">beginReadArray</a>().</p> <p>See also <a href="qsettings.html#beginReadArray">beginReadArray</a>(), <a href="qsettings.html#endArray">endArray</a>(), and <a href="qsettings.html#setArrayIndex">setArrayIndex</a>().</p> <h3 class="fn"><a name="childGroups" />QStringList QSettings.childGroups (<i>self</i>)</h3><p>Returns a list of all key top-level groups that contain keys that can be read using the <a href="qsettings.html">QSettings</a> object.</p> <p>Example:</p> <pre class="highlightedCode brush: cpp"> QSettings settings; settings.setValue("fridge/color", Qt.white); settings.setValue("fridge/size", QSize(32, 96)); settings.setValue("sofa", true); settings.setValue("tv", false); QStringList groups = settings.childGroups(); <span class="comment"> // group: ["fridge"]</span> </pre> <p>If a group is set using <a href="qsettings.html#beginGroup">beginGroup</a>(), the first-level keys in that group are returned, without the group prefix.</p> <pre class="highlightedCode brush: cpp"> settings.beginGroup("fridge"); groups = settings.childGroups(); <span class="comment"> // groups: []</span> </pre> <p>You can navigate through the entire setting hierarchy using <a href="qsettings.html#childKeys">childKeys</a>() and childGroups() recursively.</p> <p>See also <a href="qsettings.html#childKeys">childKeys</a>() and <a href="qsettings.html#allKeys">allKeys</a>().</p> <h3 class="fn"><a name="childKeys" />QStringList QSettings.childKeys (<i>self</i>)</h3><p>Returns a list of all top-level keys that can be read using the <a href="qsettings.html">QSettings</a> object.</p> <p>Example:</p> <pre class="highlightedCode brush: cpp"> QSettings settings; settings.setValue("fridge/color", Qt.white); settings.setValue("fridge/size", QSize(32, 96)); settings.setValue("sofa", true); settings.setValue("tv", false); QStringList keys = settings.childKeys(); <span class="comment"> // keys: ["sofa", "tv"]</span> </pre> <p>If a group is set using <a href="qsettings.html#beginGroup">beginGroup</a>(), the top-level keys in that group are returned, without the group prefix:</p> <pre class="highlightedCode brush: cpp"> settings.beginGroup("fridge"); keys = settings.childKeys(); <span class="comment"> // keys: ["color", "size"]</span> </pre> <p>You can navigate through the entire setting hierarchy using childKeys() and <a href="qsettings.html#childGroups">childGroups</a>() recursively.</p> <p>See also <a href="qsettings.html#childGroups">childGroups</a>() and <a href="qsettings.html#allKeys">allKeys</a>().</p> <h3 class="fn"><a name="clear" />QSettings.clear (<i>self</i>)</h3><p>Removes all entries in the primary location associated to this <a href="qsettings.html">QSettings</a> object.</p> <p>Entries in fallback locations are not removed.</p> <p>If you only want to remove the entries in the current <a href="qsettings.html#group">group</a>(), use remove("") instead.</p> <p>See also <a href="qsettings.html#remove">remove</a>() and <a href="qsettings.html#setFallbacksEnabled">setFallbacksEnabled</a>().</p> <h3 class="fn"><a name="contains" />bool QSettings.contains (<i>self</i>, QString <i>key</i>)</h3><p>Returns true if there exists a setting called <i>key</i>; returns false otherwise.</p> <p>If a group is set using <a href="qsettings.html#beginGroup">beginGroup</a>(), <i>key</i> is taken to be relative to that group.</p> <p>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 <a href="qsettings.html#section-and-key-syntax">Section and Key Syntax</a> rules.</p> <p>See also <a href="qsettings.html#value">value</a>() and <a href="qsettings.html#setValue">setValue</a>().</p> <h3 class="fn"><a name="defaultFormat" /><a href="qsettings.html#Format-enum">Format</a> QSettings.defaultFormat ()</h3><p>Returns default file format used for storing settings for the <a href="qsettings.html">QSettings</a>(<a href="qobject.html">QObject</a> *) constructor. If no default format is set, <a href="qsettings.html#Format-enum">QSettings.NativeFormat</a> is used.</p> <p>This function was introduced in Qt 4.4.</p> <p>See also <a href="qsettings.html#setDefaultFormat">setDefaultFormat</a>() and <a href="qsettings.html#format">format</a>().</p> <h3 class="fn"><a name="endArray" />QSettings.endArray (<i>self</i>)</h3><p>Closes the array that was started using <a href="qsettings.html#beginReadArray">beginReadArray</a>() or <a href="qsettings.html#beginWriteArray">beginWriteArray</a>().</p> <p>See also <a href="qsettings.html#beginReadArray">beginReadArray</a>() and <a href="qsettings.html#beginWriteArray">beginWriteArray</a>().</p> <h3 class="fn"><a name="endGroup" />QSettings.endGroup (<i>self</i>)</h3><p>Resets the group to what it was before the corresponding <a href="qsettings.html#beginGroup">beginGroup</a>() call.</p> <p>Example:</p> <pre class="highlightedCode brush: cpp"> settings.beginGroup("alpha"); <span class="comment"> // settings.group() == "alpha"</span> settings.beginGroup("beta"); <span class="comment"> // settings.group() == "alpha/beta"</span> settings.endGroup(); <span class="comment"> // settings.group() == "alpha"</span> settings.endGroup(); <span class="comment"> // settings.group() == ""</span> </pre> <p>See also <a href="qsettings.html#beginGroup">beginGroup</a>() and <a href="qsettings.html#group">group</a>().</p> <h3 class="fn"><a name="event" />bool QSettings.event (<i>self</i>, <a href="qevent.html">QEvent</a> <i>event</i>)</h3><p>Reimplemented from <a href="qobject.html#event">QObject.event</a>().</p> <h3 class="fn"><a name="fallbacksEnabled" />bool QSettings.fallbacksEnabled (<i>self</i>)</h3><p>Returns true if fallbacks are enabled; returns false otherwise.</p> <p>By default, fallbacks are enabled.</p> <p>See also <a href="qsettings.html#setFallbacksEnabled">setFallbacksEnabled</a>().</p> <h3 class="fn"><a name="fileName" />QString QSettings.fileName (<i>self</i>)</h3><p>Returns the path where settings written using this <a href="qsettings.html">QSettings</a> object are stored.</p> <p>On Windows, if the format is <a href="qsettings.html#Format-enum">QSettings.NativeFormat</a>, the return value is a system registry path, not a file path.</p> <p>See also <a href="qsettings.html#isWritable">isWritable</a>() and <a href="qsettings.html#format">format</a>().</p> <h3 class="fn"><a name="format" /><a href="qsettings.html#Format-enum">Format</a> QSettings.format (<i>self</i>)</h3><p>Returns the format used for storing the settings.</p> <p>This function was introduced in Qt 4.4.</p> <p>See also <a href="qsettings.html#defaultFormat">defaultFormat</a>(), <a href="qsettings.html#fileName">fileName</a>(), <a href="qsettings.html#scope">scope</a>(), <a href="qsettings.html#organizationName">organizationName</a>(), and <a href="qsettings.html#applicationName">applicationName</a>().</p> <h3 class="fn"><a name="group" />QString QSettings.group (<i>self</i>)</h3><p>Returns the current group.</p> <p>See also <a href="qsettings.html#beginGroup">beginGroup</a>() and <a href="qsettings.html#endGroup">endGroup</a>().</p> <h3 class="fn"><a name="iniCodec" /><a href="qtextcodec.html">QTextCodec</a> QSettings.iniCodec (<i>self</i>)</h3><p>Returns the codec that is used for accessing INI files. By default, no codec is used, so a null pointer is returned.</p> <p>This function was introduced in Qt 4.5.</p> <p>See also <a href="qsettings.html#setIniCodec">setIniCodec</a>().</p> <h3 class="fn"><a name="isWritable" />bool QSettings.isWritable (<i>self</i>)</h3><p>Returns true if settings can be written using this <a href="qsettings.html">QSettings</a> object; returns false otherwise.</p> <p>One reason why isWritable() might return false is if <a href="qsettings.html">QSettings</a> operates on a read-only file.</p> <p><b>Warning:</b> This function is not perfectly reliable, because the file permissions can change at any time.</p> <p>See also <a href="qsettings.html#fileName">fileName</a>(), <a href="qsettings.html#status">status</a>(), and <a href="qsettings.html#sync">sync</a>().</p> <h3 class="fn"><a name="organizationName" />QString QSettings.organizationName (<i>self</i>)</h3><p>Returns the organization name used for storing the settings.</p> <p>This function was introduced in Qt 4.4.</p> <p>See also <a href="qcoreapplication.html#organizationName-prop">QCoreApplication.organizationName</a>(), <a href="qsettings.html#format">format</a>(), <a href="qsettings.html#scope">scope</a>(), and <a href="qsettings.html#applicationName">applicationName</a>().</p> <h3 class="fn"><a name="remove" />QSettings.remove (<i>self</i>, QString <i>key</i>)</h3><p>Removes the setting <i>key</i> and any sub-settings of <i>key</i>.</p> <p>Example:</p> <pre class="highlightedCode brush: cpp"> QSettings settings; settings.setValue("ape"); settings.setValue("monkey", 1); settings.setValue("monkey/sea", 2); settings.setValue("monkey/doe", 4); settings.remove("monkey"); QStringList keys = settings.allKeys(); <span class="comment"> // keys: ["ape"]</span> </pre> <p>Be aware that if one of the fallback locations contains a setting with the same key, that setting will be visible after calling remove().</p> <p>If <i>key</i> is an empty string, all keys in the current <a href="qsettings.html#group">group</a>() are removed. For example:</p> <pre class="highlightedCode brush: cpp"> QSettings settings; settings.setValue("ape"); settings.setValue("monkey", 1); settings.setValue("monkey/sea", 2); settings.setValue("monkey/doe", 4); settings.beginGroup("monkey"); settings.remove(""); settings.endGroup(); QStringList keys = settings.allKeys(); <span class="comment"> // keys: ["ape"]</span> </pre> <p>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 <a href="qsettings.html#section-and-key-syntax">Section and Key Syntax</a> rules.</p> <p>See also <a href="qsettings.html#setValue">setValue</a>(), <a href="qsettings.html#value">value</a>(), and <a href="qsettings.html#contains">contains</a>().</p> <h3 class="fn"><a name="scope" /><a href="qsettings.html#Scope-enum">Scope</a> QSettings.scope (<i>self</i>)</h3><p>Returns the scope used for storing the settings.</p> <p>This function was introduced in Qt 4.4.</p> <p>See also <a href="qsettings.html#format">format</a>(), <a href="qsettings.html#organizationName">organizationName</a>(), and <a href="qsettings.html#applicationName">applicationName</a>().</p> <h3 class="fn"><a name="setArrayIndex" />QSettings.setArrayIndex (<i>self</i>, int <i>i</i>)</h3><p>Sets the current array index to <i>i</i>. Calls to functions such as <a href="qsettings.html#setValue">setValue</a>(), <a href="qsettings.html#value">value</a>(), <a href="qsettings.html#remove">remove</a>(), and <a href="qsettings.html#contains">contains</a>() will operate on the array entry at that index.</p> <p>You must call <a href="qsettings.html#beginReadArray">beginReadArray</a>() or <a href="qsettings.html#beginWriteArray">beginWriteArray</a>() before you can call this function.</p> <h3 class="fn"><a name="setDefaultFormat" />QSettings.setDefaultFormat (<a href="qsettings.html#Format-enum">Format</a> <i>format</i>)</h3><p>Sets the default file format to the given <i>format</i>, which is used for storing settings for the <a href="qsettings.html">QSettings</a>(<a href="qobject.html">QObject</a> *) constructor.</p> <p>If no default format is set, <a href="qsettings.html#Format-enum">QSettings.NativeFormat</a> is used. See the documentation for the <a href="qsettings.html">QSettings</a> constructor you are using to see if that constructor will ignore this function.</p> <p>This function was introduced in Qt 4.4.</p> <p>See also <a href="qsettings.html#defaultFormat">defaultFormat</a>() and <a href="qsettings.html#format">format</a>().</p> <h3 class="fn"><a name="setFallbacksEnabled" />QSettings.setFallbacksEnabled (<i>self</i>, bool <i>b</i>)</h3><p>Sets whether fallbacks are enabled to <i>b</i>.</p> <p>By default, fallbacks are enabled.</p> <p>See also <a href="qsettings.html#fallbacksEnabled">fallbacksEnabled</a>().</p> <h3 class="fn"><a name="setIniCodec" />QSettings.setIniCodec (<i>self</i>, <a href="qtextcodec.html">QTextCodec</a> <i>codec</i>)</h3><p>Sets the codec for accessing INI files (including <tt>.conf</tt> files on Unix) to <i>codec</i>. 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.</p> <p><b>Warning:</b> The codec must be set immediately after creating the <a href="qsettings.html">QSettings</a> object, before accessing any data.</p> <p>This function was introduced in Qt 4.5.</p> <p>See also <a href="qsettings.html#iniCodec">iniCodec</a>().</p> <h3 class="fn"><a name="setIniCodec-2" />QSettings.setIniCodec (<i>self</i>, str <i>codecName</i>)</h3><p>This is an overloaded function.</p> <p>Sets the codec for accessing INI files (including <tt>.conf</tt> files on Unix) to the <a href="qtextcodec.html">QTextCodec</a> for the encoding specified by <i>codecName</i>. Common values for <tt>codecName</tt> include "ISO 8859-1", "UTF-8", and "UTF-16". If the encoding isn't recognized, nothing happens.</p> <p>This function was introduced in Qt 4.5.</p> <p>See also <a href="qtextcodec.html#codecForName">QTextCodec.codecForName</a>().</p> <h3 class="fn"><a name="setPath" />QSettings.setPath (<a href="qsettings.html#Format-enum">Format</a> <i>format</i>, <a href="qsettings.html#Scope-enum">Scope</a> <i>scope</i>, QString <i>path</i>)</h3><p>Sets the path used for storing settings for the given <i>format</i> and <i>scope</i>, to <i>path</i>. The <i>format</i> can be a custom format.</p> <p>The table below summarizes the default values:</p> <table class="generic"> <thead> <tr class="qt-style topAlign"> <th>Platform</th> <th>Format</th> <th>Scope</th> <th>Path</th> </tr> </thead> <tr class="odd topAlign"> <td rowspan="2"> <p>Windows</p> </td> <td rowspan="2"> <p><a href="qsettings.html#Format-enum">IniFormat</a></p> </td> <td> <p><a href="qsettings.html#Scope-enum">UserScope</a></p> </td> <td> <p><tt>%APPDATA%</tt></p> </td> </tr> <tr class="even topAlign"> <td> <p><a href="qsettings.html#Scope-enum">SystemScope</a></p> </td> <td> <p><tt>%COMMON_APPDATA%</tt></p> </td> </tr> <tr class="odd topAlign"> <td rowspan="2"> <p>Unix</p> </td> <td rowspan="2"> <p><a href="qsettings.html#Format-enum">NativeFormat</a>, <a href="qsettings.html#Format-enum">IniFormat</a></p> </td> <td> <p><a href="qsettings.html#Scope-enum">UserScope</a></p> </td> <td> <p><tt>$HOME/.config</tt></p> </td> </tr> <tr class="even topAlign"> <td> <p><a href="qsettings.html#Scope-enum">SystemScope</a></p> </td> <td> <p><tt>/etc/xdg</tt></p> </td> </tr> <tr class="odd topAlign"> <td rowspan="2"> <p>Qt for Embedded Linux</p> </td> <td rowspan="2"> <p><a href="qsettings.html#Format-enum">NativeFormat</a>, <a href="qsettings.html#Format-enum">IniFormat</a></p> </td> <td> <p><a href="qsettings.html#Scope-enum">UserScope</a></p> </td> <td> <p><tt>$HOME/Settings</tt></p> </td> </tr> <tr class="even topAlign"> <td> <p><a href="qsettings.html#Scope-enum">SystemScope</a></p> </td> <td> <p><tt>/etc/xdg</tt></p> </td> </tr> <tr class="odd topAlign"> <td rowspan="2"> <p>Mac OS X</p> </td> <td rowspan="2"> <p><a href="qsettings.html#Format-enum">IniFormat</a></p> </td> <td> <p><a href="qsettings.html#Scope-enum">UserScope</a></p> </td> <td> <p><tt>$HOME/.config</tt></p> </td> </tr> <tr class="even topAlign"> <td> <p><a href="qsettings.html#Scope-enum">SystemScope</a></p> </td> <td> <p><tt>/etc/xdg</tt></p> </td> </tr> </table> <p>The default <a href="qsettings.html#Scope-enum">UserScope</a> paths on Unix and Mac OS X (<tt>$HOME/.config</tt> or $HOME/Settings) can be overridden by the user by setting the <tt>XDG_CONFIG_HOME</tt> environment variable. The default <a href="qsettings.html#Scope-enum">SystemScope</a> paths on Unix and Mac OS X (<tt>/etc/xdg</tt>) can be overridden when building the Qt library using the <tt>configure</tt> script's <tt>--sysconfdir</tt> flag (see <a href="qlibraryinfo.html">QLibraryInfo</a> for details).</p> <p>Setting the <a href="qsettings.html#Format-enum">NativeFormat</a> paths on Windows and Mac OS X has no effect.</p> <p><b>Warning:</b> This function doesn't affect existing <a href="qsettings.html">QSettings</a> objects.</p> <p>This function was introduced in Qt 4.1.</p> <p>See also <a href="qsettings.html#registerFormat">registerFormat</a>().</p> <h3 class="fn"><a name="setSystemIniPath" />QSettings.setSystemIniPath (QString <i>dir</i>)</h3><h3 class="fn"><a name="setUserIniPath" />QSettings.setUserIniPath (QString <i>dir</i>)</h3><h3 class="fn"><a name="setValue" />QSettings.setValue (<i>self</i>, QString <i>key</i>, QVariant <i>value</i>)</h3><p>Sets the value of setting <i>key</i> to <i>value</i>. If the <i>key</i> already exists, the previous value is overwritten.</p> <p>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 <a href="qsettings.html#section-and-key-syntax">Section and Key Syntax</a> rules.</p> <p>Example:</p> <pre class="highlightedCode brush: cpp"> QSettings settings; settings.setValue("interval", 30); settings.value("interval").toInt(); <span class="comment">// returns 30</span> settings.setValue("interval", 6.55); settings.value("interval").toDouble(); <span class="comment">// returns 6.55</span> </pre> <p>See also <a href="qsettings.html#value">value</a>(), <a href="qsettings.html#remove">remove</a>(), and <a href="qsettings.html#contains">contains</a>().</p> <h3 class="fn"><a name="status" /><a href="qsettings.html#Status-enum">Status</a> QSettings.status (<i>self</i>)</h3><p>Returns a status code indicating the first error that was met by <a href="qsettings.html">QSettings</a>, or <a href="qsettings.html#Status-enum">QSettings.NoError</a> if no error occurred.</p> <p>Be aware that <a href="qsettings.html">QSettings</a> delays performing some operations. For this reason, you might want to call <a href="qsettings.html#sync">sync</a>() to ensure that the data stored in <a href="qsettings.html">QSettings</a> is written to disk before calling status().</p> <p>See also <a href="qsettings.html#sync">sync</a>().</p> <h3 class="fn"><a name="sync" />QSettings.sync (<i>self</i>)</h3><p>Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in the meantime by another application.</p> <p>This function is called automatically from <a href="qsettings.html">QSettings</a>'s destructor and by the event loop at regular intervals, so you normally don't need to call it yourself.</p> <p>See also <a href="qsettings.html#status">status</a>().</p> <h3 class="fn"><a name="value" />object QSettings.value (<i>self</i>, QString <i>key</i>, QVariant <i>defaultValue</i> = QVariant(), object <i>type</i> = None)</h3><p>Returns the value for setting <i>key</i>. If the setting doesn't exist, returns <i>defaultValue</i>.</p> <p>If no default value is specified, a default <a href="qvariant.html">QVariant</a> is returned.</p> <p>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 <a href="qsettings.html#section-and-key-syntax">Section and Key Syntax</a> rules.</p> <p>Example:</p> <pre class="highlightedCode brush: cpp"> QSettings settings; settings.setValue("animal/snake", 58); settings.value("animal/snake", 1024).toInt(); <span class="comment">// returns 58</span> settings.value("animal/zebra", 1024).toInt(); <span class="comment">// returns 1024</span> settings.value("animal/zebra").toInt(); <span class="comment">// returns 0</span> </pre> <p>See also <a href="qsettings.html#setValue">setValue</a>(), <a href="qsettings.html#contains">contains</a>(), and <a href="qsettings.html#remove">remove</a>().</p> <address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.8.3 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2011</td><td align="right" width="25%">Qt 4.7.1</td></tr></table></div></address></body></html>