|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.internal.QSignalEmitterInternal
com.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.core.QObject
com.trolltech.qt.core.QSettings
public class QSettings
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.
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: QSettings settings = new QSettings("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:
QCoreApplication.setOrganizationName("MySoft"); QCoreApplication.setOrganizationDomain("mysoft.com"); QCoreApplication.setApplicationName("Star Runner"); ... QSettings settings = new QSettings();(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:
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()
:
int margin = (Integer) settings.value("editor/wrapMargin");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()
: int margin = (Integer) settings.value("editor/wrapMargin", 80);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
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:
QSettings settings = new QSettings("MySoft", "Star Runner"); QColor color = (QColor) settings.value("DataPump/bgcolor");The inverse conversion (e.g., from
QColor
to QVariant
) is automatic for all data types supported by QVariant
, including GUI-related types: QSettings settings = new QSettings("MySoft", "Star Runner"); QColor color = widget.palette().window().color(); settings.setValue("DataPump/bgcolor", color);Custom types registered using qRegisterMetaType() and qRegisterMetaTypeStreamOperators() can be stored using
QSettings
.QSettings
to '/', which makes them identical.settings.setValue("mainwindow/size", win.size()); settings.setValue("mainwindow/fullScreen", win.isFullScreen()); 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: settings.beginGroup("mainwindow"); settings.setValue("size", win.size()); settings.setValue("fullScreen", win.isFullScreen()); settings.endGroup();If a group is set using
settings.beginGroup("outputpanel"); settings.setValue("visible", panel.isVisible()); settings.endGroup();
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:
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:
QSettings obj1 = new QSettings("MySoft", "Star Runner"); QSettings obj2 = new QSettings("MySoft"); QSettings obj3 = new QSettings(QSettings.Scope.SystemScope, "MySoft", "Star Runner"); QSettings obj4 = new QSettings(QSettings.Scope.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. 1. User, Application | X | |||
2. User, Organization | o | X | ||
3. System, Application | o | X | ||
4. System, Organization | o | o | o | X |
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:
QSettings settings = new QSettings(QSettings.Format.IniFormat, QSettings.Scope.UserScope, "MySoft", "Star Runner");The Settings Editor example lets you experiment with different settings location and with fallbacks turned on or off.
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. public void writeSettings() { QSettings settings = new QSettings("Moose Soft", "Clipper"); settings.beginGroup("MainWindow"); settings.setValue("size", size()); settings.setValue("pos", pos()); settings.endGroup(); }See Window Geometry for a discussion on why it is better to call
public void readSettings() { QSettings settings = new QSettings("Moose Soft", "Clipper"); settings.beginGroup("MainWindow"); resize((QSize) settings.value("size", new QSize(400, 400))); move((QPoint) settings.value("pos", new QPoint(200, 200))); settings.endGroup(); }
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:
public MainWindow() { ... readSettings(); }See the Application example for a self-contained example that uses
protected void closeEvent(QCloseEvent event) { if (letsCutItOut()) { writeSettings(); event.accept(); } else { event.ignore(); } }
QSettings
.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. Changes performed by another process aren't visible in the current process until sync()
is called.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 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:
NativeFormat
settings are stored in the following registry paths: IniFormat
, the following files are used on Unix and Mac OS X: 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:
QSettings settings = new QSettings("/home/petra/misc/myapp.ini", QSettings.Format.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:
QSettings settings = new QSettings("/Users/petra/misc/myapp.plist", QSettings.Format.NativeFormat);
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:
QSettings settings = new QSettings("HKEY_CURRENT_USER\\Software\\Microsoft\\Office", QSettings.Format.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: 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.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"); // returns "Milkyway"On other platforms than windows, "Default" and "." would be treated as regular subkeys.
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: IniFormat
instead of the NativeFormat
.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.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: if (QSysInfo.operatingSystem() == QSysInfo.OS_DARWIN) { QSettings settings = new QSettings("grenoullelogique.fr", "Squash"); } else { QSettings settings = new QSettings("Grenoulle Logique", "Squash"); }
QVariant
, QSessionManager
, Settings Editor Example, and Application Example.
Nested Class Summary | |
---|---|
static class |
QSettings.Format
|
static class |
QSettings.Scope
This enum specifies whether settings are user-specific or shared by all users of the same system. |
static class |
QSettings.Status
This enum describes the current status of the text stream. |
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter |
---|
QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9 |
Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal |
Field Summary |
---|
Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
currentSender |
Constructor Summary | |
---|---|
QSettings()
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() . |
|
QSettings(QObject parent)
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() . |
|
QSettings(QSettings.Format format,
QSettings.Scope scope,
java.lang.String organization)
Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent. |
|
QSettings(QSettings.Format format,
QSettings.Scope scope,
java.lang.String organization,
java.lang.String application)
Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent. |
|
QSettings(QSettings.Format format,
QSettings.Scope scope,
java.lang.String organization,
java.lang.String application,
QObject parent)
Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent. |
|
QSettings(QSettings.Scope scope,
java.lang.String organization)
Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent. |
|
QSettings(QSettings.Scope scope,
java.lang.String organization,
java.lang.String application)
Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent. |
|
QSettings(QSettings.Scope scope,
java.lang.String organization,
java.lang.String application,
QObject parent)
Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent. |
|
QSettings(java.lang.String organization)
Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent. |
|
QSettings(java.lang.String fileName,
QSettings.Format format)
Constructs a QSettings object for accessing the settings stored in the file called fileName, with parent parent. |
|
QSettings(java.lang.String fileName,
QSettings.Format format,
QObject parent)
Constructs a QSettings object for accessing the settings stored in the file called fileName, with parent parent. |
|
QSettings(java.lang.String organization,
java.lang.String application)
Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent. |
|
QSettings(java.lang.String organization,
java.lang.String application,
QObject parent)
Constructs a QSettings object for accessing settings of the application called application from the organization called organization, and with parent parent. |
Method Summary | |
---|---|
java.util.List |
allKeys()
Returns a list of all keys, including subkeys, that can be read using the QSettings object. |
java.lang.String |
applicationName()
Returns the application name used for storing the settings. |
void |
beginGroup(java.lang.String prefix)
Appends prefix to the current group. |
int |
beginReadArray(java.lang.String prefix)
Adds prefix to the current group and starts reading from an array. |
void |
beginWriteArray(java.lang.String prefix)
Adds prefix to the current group and starts writing an array of size size. |
void |
beginWriteArray(java.lang.String prefix,
int size)
Adds prefix to the current group and starts writing an array of size size. |
java.util.List |
childGroups()
Returns a list of all key top-level groups that contain keys that can be read using the QSettings object. |
java.util.List |
childKeys()
Returns a list of all top-level keys that can be read using the QSettings object. |
void |
clear()
Removes all entries in the primary location associated to this QSettings object. |
boolean |
contains(java.lang.String key)
Returns true if there exists a setting called key; returns false otherwise. |
static QSettings.Format |
defaultFormat()
Returns default file format used for storing settings for the QSettings (QObject *) constructor. |
void |
endArray()
Closes the array that was started using beginReadArray() or beginWriteArray() . |
void |
endGroup()
Resets the group to what it was before the corresponding beginGroup() call. |
boolean |
fallbacksEnabled()
Returns true if fallbacks are enabled; returns false otherwise. |
java.lang.String |
fileName()
Returns the path where settings written using this QSettings object are stored. |
QSettings.Format |
format()
Returns the format used for storing the settings. |
java.lang.String |
group()
Returns the current group. |
boolean |
isWritable()
Returns true if settings can be written using this QSettings object; returns false otherwise. |
java.lang.String |
organizationName()
Returns the organization name used for storing the settings. |
void |
remove(java.lang.String key)
Removes the setting key and any sub-settings of key. |
QSettings.Scope |
scope()
Returns the scope used for storing the settings. |
void |
setArrayIndex(int i)
Sets the current array index to i. |
static void |
setDefaultFormat(QSettings.Format format)
Sets the default file format to the given format, used for storing settings for the QSettings (QObject *) constructor. |
void |
setFallbacksEnabled(boolean b)
Sets whether fallbacks are enabled to b. |
static void |
setPath(QSettings.Format format,
QSettings.Scope scope,
java.lang.String path)
Sets the path used for storing settings for the given format and scope, to path. |
void |
setValue(java.lang.String key,
java.lang.Object value)
Sets the value of setting key to value. |
QSettings.Status |
status()
Returns a status code indicating the first error that was met by QSettings , or QSettings::NoError if no error occurred. |
void |
sync()
Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in the meantime by another application. |
java.lang.Object |
value(java.lang.String key)
Returns the value for setting key. |
java.lang.Object |
value(java.lang.String key,
java.lang.Object defaultValue)
Returns the value for setting key. |
Methods inherited from class com.trolltech.qt.core.QObject |
---|
childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, indexOfProperty, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, properties, property, removeEventFilter, setObjectName, setParent, setProperty, startTimer, timerEvent, toString, userProperty |
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread |
Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
__qt_signalInitialization |
Methods inherited from class java.lang.Object |
---|
clone, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Constructor Detail |
---|
public QSettings()
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).
The code
QSettings settings = new QSettings("Moose Soft", "Facturo-Pro");is equivalent to
QCoreApplication.setOrganizationName("Moose Soft"); QCoreApplication.setApplicationName("Facturo-Pro"); QSettings settings = new QSettings();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.
QCoreApplication::setOrganizationName()
, QCoreApplication::setOrganizationDomain()
, QCoreApplication::setApplicationName()
, and setDefaultFormat()
.
public QSettings(QObject parent)
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).
The code
QSettings settings = new QSettings("Moose Soft", "Facturo-Pro");is equivalent to
QCoreApplication.setOrganizationName("Moose Soft"); QCoreApplication.setApplicationName("Facturo-Pro"); QSettings settings = new QSettings();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.
QCoreApplication::setOrganizationName()
, QCoreApplication::setOrganizationDomain()
, QCoreApplication::setApplicationName()
, and setDefaultFormat()
.
public QSettings(QSettings.Format format, QSettings.Scope scope, java.lang.String organization, java.lang.String application)
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
.
public QSettings(QSettings.Format format, QSettings.Scope scope, java.lang.String organization)
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
.
public QSettings(QSettings.Format format, QSettings.Scope scope, java.lang.String organization, java.lang.String application, QObject parent)
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
.
public QSettings(QSettings.Scope scope, java.lang.String organization, java.lang.String application)
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 QSettings::NativeFormat
.
If no application name is given, the QSettings
object will only access the organization-wide locations
.
setDefaultFormat()
.
public QSettings(QSettings.Scope scope, java.lang.String organization)
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 QSettings::NativeFormat
.
If no application name is given, the QSettings
object will only access the organization-wide locations
.
setDefaultFormat()
.
public QSettings(QSettings.Scope scope, java.lang.String organization, java.lang.String application, QObject parent)
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 QSettings::NativeFormat
.
If no application name is given, the QSettings
object will only access the organization-wide locations
.
setDefaultFormat()
.
public QSettings(java.lang.String fileName, QSettings.Format format)
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.)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.fileName()
.
public QSettings(java.lang.String fileName, QSettings.Format format, QObject parent)
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.)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.fileName()
.
public QSettings(java.lang.String organization, java.lang.String application)
QSettings
object for accessing settings of the application called application from the organization called organization, and with parent parent. Example:
QSettings settings = new QSettings("Moose Tech", "Facturo-Pro");The scope is
QSettings::UserScope
and the format is QSettings::NativeFormat
. setDefaultFormat()
, and Fallback Mechanism
.
public QSettings(java.lang.String organization)
QSettings
object for accessing settings of the application called application from the organization called organization, and with parent parent. Example:
QSettings settings = new QSettings("Moose Tech", "Facturo-Pro");The scope is
QSettings::UserScope
and the format is QSettings::NativeFormat
. setDefaultFormat()
, and Fallback Mechanism
.
public QSettings(java.lang.String organization, java.lang.String application, QObject parent)
QSettings
object for accessing settings of the application called application from the organization called organization, and with parent parent. Example:
QSettings settings = new QSettings("Moose Tech", "Facturo-Pro");The scope is
QSettings::UserScope
and the format is QSettings::NativeFormat
. setDefaultFormat()
, and Fallback Mechanism
.
Method Detail |
---|
public final java.util.List allKeys()
QSettings
object. Example:
QSettings settings = new QSettings(); settings.setValue("fridge/color", Qt.GlobalColor.white); settings.setValue("fridge/size", new QSize(32, 96)); settings.setValue("sofa", true); settings.setValue("tv", false); List<String> keys = settings.allKeys(); // 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: settings.beginGroup("fridge"); keys = settings.allKeys(); // keys: ["color", "size"]
childGroups()
, and childKeys()
.
public final java.lang.String applicationName()
QCoreApplication::applicationName()
, format()
, scope()
, and organizationName()
.
public final void beginGroup(java.lang.String prefix)
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:
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();This will set the value of three settings:
endGroup()
to reset the current group to what it was before the corresponding beginGroup()
call. Groups can be nested. endGroup()
, and group()
.
public final int beginReadArray(java.lang.String prefix)
Example:
class Login { public String userName; public String password; }; List<Login> logins = new ArrayList<Login>(); // ... QSettings settings = new QSettings(); int size = settings.beginReadArray("logins"); for (int i = 0; i < size; ++i) { settings.setArrayIndex(i); Login login = new Login(); login.userName = settings.value("userName").toString(); login.password = settings.value("password").toString(); logins.add(login); } settings.endArray();Use
beginWriteArray()
to write the array in the first place. beginWriteArray()
, endArray()
, and setArrayIndex()
.
public final void beginWriteArray(java.lang.String prefix)
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:
class Login { String userName; String password; }; List<Login> logins = new ArrayList<Login>(); // ... QSettings settings = new QSettings(); settings.beginWriteArray("logins"); for (int i = 0; i < logins.size(); ++i) { settings.setArrayIndex(i); settings.setValue("userName", logins.get(i).userName); settings.setValue("password", logins.get(i).password); } settings.endArray();The generated keys will have the form
beginReadArray()
. beginReadArray()
, endArray()
, and setArrayIndex()
.
public final void beginWriteArray(java.lang.String prefix, int size)
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:
class Login { String userName; String password; }; List<Login> logins = new ArrayList<Login>(); // ... QSettings settings = new QSettings(); settings.beginWriteArray("logins"); for (int i = 0; i < logins.size(); ++i) { settings.setArrayIndex(i); settings.setValue("userName", logins.get(i).userName); settings.setValue("password", logins.get(i).password); } settings.endArray();The generated keys will have the form
beginReadArray()
. beginReadArray()
, endArray()
, and setArrayIndex()
.
public final java.util.List childGroups()
QSettings
object. Example:
QSettings settings = new QSettings(); settings.setValue("fridge/color", Qt.GlobalColor.white); settings.setValue("fridge/size", new QSize(32, 96)); settings.setValue("sofa", true); settings.setValue("tv", false); List<String> groups = settings.childGroups(); // group: ["fridge"]If a group is set using
beginGroup()
, the first-level keys in that group are returned, without the group prefix. settings.beginGroup("fridge"); groups = settings.childGroups(); // groups: []You can navigate through the entire setting hierarchy using
childKeys()
and childGroups()
recursively. childKeys()
, and allKeys()
.
public final java.util.List childKeys()
QSettings
object. Example:
QSettings settings = new QSettings(); settings.setValue("fridge/color", Qt.GlobalColor.white); settings.setValue("fridge/size", new QSize(32, 96)); settings.setValue("sofa", true); settings.setValue("tv", false); List<String> keys = settings.childKeys(); // keys: ["sofa", "tv"]If a group is set using
beginGroup()
, the top-level keys in that group are returned, without the group prefix: settings.beginGroup("fridge"); keys = settings.childKeys(); // keys: ["color", "size"]You can navigate through the entire setting hierarchy using
childKeys()
and childGroups()
recursively. childGroups()
, and allKeys()
.
public final void clear()
QSettings
object. Entries in fallback locations are not removed.
If you only want to remove the entries in the current group()
, use remove("") instead.
remove()
, and setFallbacksEnabled()
.
public final boolean contains(java.lang.String key)
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 Key Syntax
rules.
value()
, and setValue()
.
public final void endArray()
beginReadArray()
or beginWriteArray()
. beginReadArray()
, and beginWriteArray()
.
public final void endGroup()
beginGroup()
call. Example:
settings.beginGroup("alpha"); // settings.group() == "alpha" settings.beginGroup("beta"); // settings.group() == "alpha/beta" settings.endGroup(); // settings.group() == "alpha" settings.endGroup(); // settings.group() == ""
beginGroup()
, and group()
.
public final boolean fallbacksEnabled()
By default, fallbacks are enabled.
setFallbacksEnabled()
.
public final java.lang.String fileName()
QSettings
object are stored. On Windows, if the format is QSettings::NativeFormat
, the return value is a system registry path, not a file path.
isWritable()
, and format()
.
public final QSettings.Format format()
defaultFormat()
, fileName()
, scope()
, organizationName()
, and applicationName()
.
public final java.lang.String group()
beginGroup()
, and endGroup()
.
public final boolean isWritable()
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.
fileName()
, status()
, and sync()
.
public final java.lang.String organizationName()
QCoreApplication::organizationName()
, format()
, scope()
, and applicationName()
.
public final void remove(java.lang.String key)
Example:
QSettings settings = new QSettings(); settings.setValue("ape", 0); settings.setValue("monkey", 1); settings.setValue("monkey/sea", 2); settings.setValue("monkey/doe", 4); settings.remove("monkey"); List<String> keys = settings.allKeys(); // 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:
QSettings settings = new QSettings(); settings.setValue("ape", 0); settings.setValue("monkey", 1); settings.setValue("monkey/sea", 2); settings.setValue("monkey/doe", 4); settings.beginGroup("monkey"); settings.remove(""); settings.endGroup(); List<String> keys = settings.allKeys(); // 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
Key Syntax
rules. setValue()
, value()
, and contains()
.
public final QSettings.Scope scope()
format()
, organizationName()
, and applicationName()
.
public final void setArrayIndex(int i)
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.
public final void setFallbacksEnabled(boolean b)
By default, fallbacks are enabled.
fallbacksEnabled()
.
public final void setValue(java.lang.String key, java.lang.Object value)
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 Key Syntax
rules.
Example:
QSettings settings = new QSettings(); settings.setValue("interval", 30); QVariant.toInt(settings.value("interval")); // returns 30 settings.setValue("interval", 6.55); QVariant.toDouble(settings.value("interval")); // returns 6.55
value()
, remove()
, and contains()
.
public final QSettings.Status status()
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()
.
sync()
.
public final void sync()
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.
status()
.
public final java.lang.Object value(java.lang.String key)
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 Key Syntax
rules.
Example:
QSettings settings = new QSettings(); settings.setValue("animal/snake", 58); QVariant.toInt(settings.value("animal/snake", 1024)); // returns 58 QVariant.toInt(settings.value("animal/zebra", 1024)); // returns 1024 QVariant.toInt(settings.value("animal/zebra")); // returns 0
setValue()
, contains()
, and remove()
.
public final java.lang.Object value(java.lang.String key, java.lang.Object 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 Key Syntax
rules.
Example:
QSettings settings = new QSettings(); settings.setValue("animal/snake", 58); QVariant.toInt(settings.value("animal/snake", 1024)); // returns 58 QVariant.toInt(settings.value("animal/zebra", 1024)); // returns 1024 QVariant.toInt(settings.value("animal/zebra")); // returns 0
setValue()
, contains()
, and remove()
.
public static QSettings.Format defaultFormat()
QSettings
(QObject
*) constructor. If no default format is set, QSettings::NativeFormat
is used. setDefaultFormat()
, and format()
.
public static void setDefaultFormat(QSettings.Format format)
QSettings
(QObject
*) constructor. If no default format is set, QSettings::NativeFormat
is used.
defaultFormat()
, and format()
.
public static void setPath(QSettings.Format format, QSettings.Scope scope, java.lang.String path)
The table below summarizes the default values:
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 |
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.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |