PySide.QtCore.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.
PySide.QtCore.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 .
PySide.QtCore.QSettings ‘s API is based on PySide.QtCore.QVariant ,允许保存大多数基于值的类型,譬如 PySide.QtCore.QString , PySide.QtCore.QRect ,和 PySide.QtGui.QImage ,采用最少努力。
若需要的全是基于内存的非持久性结构,请考虑使用 QMap < PySide.QtCore.QString , PySide.QtCore.QVariant > 代替。
当创建 PySide.QtCore.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 PySide.QtCore.QSettings object as follows:
settings = QSettings("MySoft", "Star Runner")
PySide.QtCore.QSettings objects can be created either on the stack or on the heap (i.e. using new ). Constructing and destroying a PySide.QtCore.QSettings object is very fast.
若使用 PySide.QtCore.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 PySide.QtCore.QSettings 构造函数:
QCoreApplication.setOrganizationName("MySoft")
QCoreApplication.setOrganizationDomain("mysoft.com")
QCoreApplication.setApplicationName("Star Runner")
...
settings = 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.)
PySide.QtCore.QSettings stores settings. Each setting consists of a PySide.QtCore.QString 指定设置的名称 ( key ) 和 PySide.QtCore.QVariant 存储键关联数据。要写入设置,使用 PySide.QtCore.QSettings.setValue() 。例如:
settings.setValue("editor/wrapMargin", 68)
若已经存在具有相同键的设置,则现有值被新值所覆写。为提高效率,改变可能不会被立即保存到永久存储。(可以始终调用 PySide.QtCore.QSettings.sync() to commit your changes.)
可以获取设置的值使用 PySide.QtCore.QSettings.value() :
margin = int(settings.value("editor/wrapMargin"))
If there is no setting with the specified name, PySide.QtCore.QSettings returns a null PySide.QtCore.QVariant (其可以被转换成整数 0)。可以指定另一默认值通过把第 2 自变量传递给 PySide.QtCore.QSettings.value() :
margin = int(settings.value("editor/wrapMargin", 80))
要测试给定键是否存在,调用 PySide.QtCore.QSettings.contains() . To remove the setting associated with a key, call PySide.QtCore.QSettings.remove() . To obtain the list of all keys, call PySide.QtCore.QSettings.allKeys() . To remove all keys, call PySide.QtCore.QSettings.clear() .
因为 PySide.QtCore.QVariant 属于 QtCore library, it cannot provide conversion functions to data types such as PySide.QtGui.QColor , PySide.QtGui.QImage ,和 PySide.QtGui.QPixmap , which are part of QtGui . In other words, there is no toColor() , toImage() ,或 toPixmap() 函数在 PySide.QtCore.QVariant .
相反,可以使用 QVariant.value() 或 qVariantValue() template function. For example:
settings = QSettings("MySoft", "Star Runner")
color = QColor(settings.value("DataPump/bgcolor"))
反向转换 (如,从 PySide.QtGui.QColor to PySide.QtCore.QVariant ) 对于所有支持数据类型是自动通过 PySide.QtCore.QVariant ,包括 GUI 相关类型:
settings = QSettings("MySoft", "Star Runner")
color = palette().background().color()
settings.setValue("DataPump/bgcolor", color)
自定义类型注册采用 qRegisterMetaType() and qRegisterMetaTypeStreamOperators() can be stored using PySide.QtCore.QSettings .
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:
You can form hierarchical keys using the ‘/' character as a separator, similar to Unix file paths. For example:
settings.setValue("mainwindow/size", win.size())
settings.setValue("mainwindow/fullScreen", win.isFullScreen())
settings.setValue("outputpanel/visible", panel.isVisible())
若希望保存 (或还原) 许多采用相同前缀的设置,可以指定前缀使用 PySide.QtCore.QSettings.beginGroup() 和调用 PySide.QtCore.QSettings.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()
settings.beginGroup("outputpanel")
settings.setValue("visible", panel.isVisible())
settings.endGroup()
若设置组使用 PySide.QtCore.QSettings.beginGroup() , the behavior of most functions changes consequently. Groups can be set recursively.
In addition to groups, PySide.QtCore.QSettings also supports an “array” concept. See PySide.QtCore.QSettings.beginReadArray() and PySide.QtCore.QSettings.beginWriteArray() 了解细节。
Let's assume that you have created a PySide.QtCore.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:
(See Platform-Specific Notes below for information on what these locations are on the different platforms supported by Qt.)
If a key cannot be found in the first location, the search goes on in the second location, and so on. This enables you to store system-wide or organization-wide settings and to override them on a per-user or per-application basis. To turn off this mechanism, call setFallbacksEnabled(false).
虽然来自所有 4 个位置的键都可用于读取,但仅第一个文件 (为手中应用程序的特定用户位置) 可写访问。要写入任何其它文件,省略应用程序名称和/或指定 QSettings.SystemScope (而不是 QSettings.UserScope ,默认)。
让我们看下范例:
obj1 = QSettings("MySoft", "Star Runner")
obj2 = QSettings("MySoft")
obj3 = QSettings(QSettings.SystemScope, "MySoft", "Star Runner")
obj4 = QSettings(QSettings.SystemScope, "MySoft")
The table below summarizes which PySide.QtCore.QSettings objects access which location. “ X ” means that the location is the main location associated to the PySide.QtCore.QSettings object and is used both for reading and for writing; “o” means that the location is used as a fallback when reading.
| Locations | obj1 | obj2 | obj3 | obj4 |
|
X | |||
|
o | X | ||
|
o | X | ||
|
o | o | o | X |
这种机制的妙处是它工作于由 Qt 支持的所有平台,且仍然给予很大灵活性,不要求指定任何文件名 (或注册表路径)。
若希望在所有平台使用 INI 文件而不是本机 API,可以传递 QSettings.IniFormat as the first argument to the PySide.QtCore.QSettings constructor, followed by the scope, the organization name, and the application name:
settings = QSettings(QSettings.IniFormat, QSettings.UserScope,
"MySoft", "Star Runner")
设置编辑器 范例允许您试验不同设置位置,及启用或禁用回退。
PySide.QtCore.QSettings is often used to store the state of a GUI application. The following example illustrates how to use PySide.QtCore.QSettings to save and restore the geometry of an application's main window.
class MainWindow(QMainWindow):
...
def writeSettings(self):
self.settings = QSettings("Moose Soft", "Clipper")
self.settings.beginGroup("MainWindow")
self.settings.setValue("size", self.size())
self.settings.setValue("pos", self.pos())
self.settings.endGroup()
def readSettings(self):
self.settings = QSettings("Moose Soft", "Clipper")
self.settings.beginGroup("MainWindow")
self.resize(settings.value("size", QSize(400, 400)).toSize())
self.move(settings.value("pos", QPoint(200, 200)).toPoint())
self.settings.endGroup()
见 窗口几何体 了解为什么更好的论述,调用 QWidget.resize() and QWidget.move() 而不是 QWidget.setGeometry() to restore a window's geometry.
readSettings() and writeSettings() 函数必须从主窗口的构造函数调用,关闭事件处理程序如下所示:
def __init__(self):
self.settings = None
...
self.readSettings()
# event : QCloseEvent
def closeEvent(self, event):
if self.userReallyWantsToQuit():
self.writeSettings()
event.accept()
else:
event.ignore()
见 应用程序 example for a self-contained example that uses PySide.QtCore.QSettings .
PySide.QtCore.QSettings is 可重入 . This means that you can use distinct PySide.QtCore.QSettings object in different threads simultaneously. This guarantee stands even when the PySide.QtCore.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 PySide.QtCore.QSettings object, the change will immediately be visible in any other PySide.QtCore.QSettings objects that operate on the same location and that live in the same process.
PySide.QtCore.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 PySide.QtCore.QSettings.sync() imports changes made by other processes (in addition to writing the changes from this PySide.QtCore.QSettings ).
作为提及在 Fallback Mechanism section, PySide.QtCore.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.
在 Unix 系统,若文件格式为 NativeFormat ,默认使用下列文件:
On Mac OS X versions 10.2 and 10.3, these files are used by default:
在 Windows, NativeFormat 设置存储在下列注册表路径:
注意
在 Windows,对于运行在 WOW64 模式下的 32 位程序,设置存储在下列注册表路径: HKEY_LOCAL_MACHINE\Software\WOW6432node .
若文件格式为 IniFormat , the following files are used on Unix and Mac OS X:
在 Windows,使用下列文件:
%APPDATA% path is usually C:\Documents and Settings\*User Name*\Application 数据 ; the %COMMON_APPDATA% path is usually C:\Documents and Settings\All Users\Application 数据 .
On Symbian, the following files are used for both IniFormat and NativeFormat (in this example, we assume that the application is installed on the e-drive and its Secure ID is 0xECB00931 ):
SystemScope settings location is determined from the installation drive and Secure ID (UID3) of the application. If the application is built-in on the ROM, the drive used for SystemScope is c: .
注意
Symbian SystemScope settings are by default private to the application and not shared between applications, unlike other environments.
路径对于 .ini and .conf 文件可以改变使用 PySide.QtCore.QSettings.setPath() . On Unix and Mac OS X, the user can override them by by setting the XDG_CONFIG_HOME 环境变量;见 PySide.QtCore.QSettings.setPath() 了解细节。
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 PySide.QtCore.QSettings constructor that takes a file name as first argument and pass QSettings.IniFormat 作为第 2 自变量。例如:
settings = QSettings("/home/petra/misc/myapp.ini",
QSettings.IniFormat)
You can then use the PySide.QtCore.QSettings object to read and write settings in the file.
On Mac OS X, you can access XML-based .plist 文件通过传递 QSettings.NativeFormat 作为第 2 自变量。例如:
settings = QSettings("/Users/petra/misc/myapp.plist",
QSettings.NativeFormat)
在 Windows, PySide.QtCore.QSettings lets you access settings that have been written with PySide.QtCore.QSettings (or settings in a supported format, e.g., string data) in the system registry. This is done by constructing a PySide.QtCore.QSettings object with a path in the registry and QSettings.NativeFormat .
例如:
settings = QSettings("HKEY_CURRENT_USER\\Software\\Microsoft\\Office",
QSettings.NativeFormat)
All the registry entries that appear under the specified path can be read or written through the PySide.QtCore.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 PySide.QtCore.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.
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:
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.
UserScope settings in Symbian are writable by any application by default. To protect the application settings from access and tampering by other applications, the settings need to be placed in the private secure area of the application. This can be done by specifying the settings storage path directly to the private area. The following snippet changes the UserScope to c:/private/ecb00931/MySoft.conf (provided the application is installed on the c-drive and its Secure ID is 0xECB00931 :
<Code snippet "doc/src/snippets/code/src_corelib_io_qsettings.cpp:30" not found>
Framework libraries (like Qt itself) may store configuration and cache settings using UserScope , which is accessible and writable by other applications. If the application is very security sensitive or uses high platform security capabilities, it may be prudent to also force framework settings to be stored in the private directory of the application. This can be done by changing the default path of UserScope before PySide.QtGui.QApplication is created:
<Code snippet "doc/src/snippets/code/src_corelib_io_qsettings.cpp:31" not found>
Note that this may affect framework libraries' functionality if they expect the settings to be shared between applications.
On Mac OS X, the global Qt settings (stored in com.trolltech.plist ) are stored in the application settings file in two situations:
In these situations, the application settings file is named using the bundle identifier of the application, which must consequently be set in the application's Info.plist 文件。
This feature is provided to ease the acceptance of Qt applications into the Mac App Store, as the default behaviour of storing global Qt settings in the com.trolltech.plist file does not conform with Mac App Store file system usage requirements. For more information about submitting Qt applications to the Mac App Store, see Preparing a Qt application for Mac App Store submission .
While PySide.QtCore.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:
另请参阅
PySide.QtCore.QVariant PySide.QtGui.QSessionManager 设置编辑器范例 应用程序范例
| 参数: |
|
|---|
构造 PySide.QtCore.QSettings object for accessing settings of the application and organization set previously with a call to QCoreApplication.setOrganizationName() , QCoreApplication.setOrganizationDomain() ,和 QCoreApplication.setApplicationName() .
作用域是 QSettings.UserScope 和格式为 PySide.QtCore.QSettings.defaultFormat() ( QSettings.NativeFormat 默认情况下)。使用 PySide.QtCore.QSettings.setDefaultFormat() before calling this constructor to change the default format used by this constructor.
代码
settings = QSettings("Moose Soft", "Facturo-Pro")
相当于
QCoreApplication.setOrganizationName("Moose Soft")
QCoreApplication.setApplicationName("Facturo-Pro")
settings = QSettings()
若 QCoreApplication.setOrganizationName() and QCoreApplication.setApplicationName() has not been previously called, the PySide.QtCore.QSettings object will not be able to read or write any settings, and PySide.QtCore.QSettings.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() PySide.QtCore.QSettings.setDefaultFormat()
构造 PySide.QtCore.QSettings object for accessing settings of the application called application 从组织称为 organization ,和采用父级 parent .
若 scope is QSettings.UserScope , PySide.QtCore.QSettings object searches user-specific settings first, before it searches system-wide settings as a fallback. If scope is QSettings.SystemScope , PySide.QtCore.QSettings object ignores user-specific settings and provides access to system-wide settings.
若 format is QSettings.NativeFormat ,本机 API 用于存储设置。若 format is QSettings.IniFormat ,使用 INI 格式。
If no application name is given, the PySide.QtCore.QSettings object will only access the organization-wide locations .
构造 PySide.QtCore.QSettings object for accessing settings of the application called application 从组织称为 organization ,和采用父级 parent .
若 scope is QSettings.UserScope , PySide.QtCore.QSettings object searches user-specific settings first, before it searches system-wide settings as a fallback. If scope is QSettings.SystemScope , PySide.QtCore.QSettings object ignores user-specific settings and provides access to system-wide settings.
存储格式被设为 QSettings.NativeFormat (即:调用 PySide.QtCore.QSettings.setDefaultFormat() before calling this constructor has no effect).
If no application name is given, the PySide.QtCore.QSettings object will only access the organization-wide locations .
构造 PySide.QtCore.QSettings object for accessing the settings stored in the file called fileName ,采用父级 parent 。若文件不存在,创建它。
若 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 文件。在 Windows, fileName 是系统注册表路径。
若 format is QSettings.IniFormat , fileName 是 INI 文件的名称。
警告
提供此函数是为了方便。它可以很好地访问 INI 或 .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:
构造 PySide.QtCore.QSettings object for accessing settings of the application called application 从组织称为 organization ,和采用父级 parent .
范例:
settings = QSettings("Moose Tech", "Facturo-Pro")
作用域设置为 QSettings.UserScope ,和格式设置为 QSettings.NativeFormat (即:调用 PySide.QtCore.QSettings.setDefaultFormat() before calling this constructor has no effect).
另请参阅
PySide.QtCore.QSettings.setDefaultFormat() Fallback Mechanism
此枚举类型指定存储格式,用于 PySide.QtCore.QSettings .
| 常量 | 描述 |
|---|---|
| QSettings.NativeFormat | Store the settings using the most appropriate storage format for the platform. On Windows, this means the system registry; on Mac OS X, this means the CFPreferences API; on Unix, this means textual configuration files in INI format. |
| QSettings.IniFormat | Store the settings in INI files. |
| QSettings.InvalidFormat | 返回的特殊值由 registerFormat() . |
On Unix, NativeFormat and IniFormat mean the same thing, except that the file extension is different ( .conf for NativeFormat , .ini for IniFormat ).
INI 文件格式是在所有平台 Qt 所支持的 Windows 文件格式。在缺乏 INI 标准的情况下,我们试着遵循 Microsoft 做法,但有以下例外:
若存储类型 PySide.QtCore.QVariant 无法转换为 PySide.QtCore.QString (如, PySide.QtCore.QPoint , PySide.QtCore.QRect ,和 PySide.QtCore.QSize ),Qt 使用 @ 基句法以编码类型。例如:
pos = @Point(100 100)
为最小化兼容性问题,任何 @ 未出现在值第一位置或之后未紧跟 Qt 类型 ( Point , Rect , Size ,等) 被视为正常字符。
尽管反斜杠是 INI 文件中的特殊字符,但大多数 Windows 应用程序不转义反斜杠 ( \ ) 在文件路径:
windir = C:\Windows
PySide.QtCore.QSettings 始终视反斜杠为特殊字符,且不提供用于读取或写入这种条目的 API。
INI 文件格式对键句法有严格限定。Qt 要解决这是通过使用 % as an escape character in keys. In addition, if you save a top-level setting (a key with no slashes in it, e.g., “someKey”), it will appear in the INI file's “General” section. To avoid overwriting other keys, if you save something using the a key such as “General/someKey”, the key will be located in the “%General” section, not in the “General” section.
遵循应该在接受的东西上是自由派,而在产生的东西上是保守派的哲学, PySide.QtCore.QSettings 将接受 Latin-1 编码 INI 文件,但生成纯 ASCII 文件,使用标准 INI 转义序列编码其中的非 ASCII 值。要使 INI 文件更可读 (但潜在不太兼容),调用 PySide.QtCore.QSettings.setIniCodec() .
另请参阅
registerFormat() PySide.QtCore.QSettings.setPath()
下列状态值是可能的:
| 常量 | 描述 |
|---|---|
| QSettings.NoError | 没有发生错误。 |
| QSettings.AccessError | 发生访问错误 (如:试着写入只读文件)。 |
| QSettings.FormatError | 发生格式错误 (如:加载畸形 INI 文件)。 |
此枚举指定设置是具体用户,还是由同一系统的所有用户共享。
| 常量 | 描述 |
|---|---|
| QSettings.UserScope | 将设置存储到当前用户的特定位置 (如:在用户主目录中)。 |
| QSettings.SystemScope | 将设置存储在全局位置,以便同一计算机中的所有用户访问相同设置集。 |
| 返回类型: | 字符串列表 |
|---|
返回所有键的列表 (包括子键),可以被读取使用 PySide.QtCore.QSettings 对象。
范例:
settings = QSettings()
settings.setValue("fridge/color", Qt.white)
settings.setValue("fridge/size", QSize(32, 96))
settings.setValue("sofa", True)
settings.setValue("tv", False)
keys = settings.allKeys();
# keys: ["fridge/color", "fridge/size", "sofa", "tv"]
若设置组使用 PySide.QtCore.QSettings.beginGroup() , only the keys in the group are returned, without the group prefix:
settings.beginGroup("fridge")
keys = settings.allKeys()
# keys: ["color", "size"]
| 返回类型: | unicode |
|---|
返回用于存储设置的应用程序名称。
| 参数: | prefix – unicode |
|---|
追加 prefix 到当前组。
当前组自动前置所有指定键到 PySide.QtCore.QSettings 。此外,查询函数譬如 PySide.QtCore.QSettings.childGroups() , PySide.QtCore.QSettings.childKeys() ,和 PySide.QtCore.QSettings.allKeys() are based on the group. By default, no group is set.
组对避免一遍又一遍地键入相同设置路径很有用。例如:
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()
这将设置 3 个设置值:
调用 PySide.QtCore.QSettings.endGroup() to reset the current group to what it was before the corresponding PySide.QtCore.QSettings.beginGroup() call. Groups can be nested.
| 参数: | prefix – unicode |
|---|---|
| 返回类型: | PySide.QtCore.int |
添加 prefix to the current group and starts reading from an array. Returns the size of the array.
范例:
class Login:
userName = ''
password = ''
logins = []
...
settings = QSettings()
size = settings.beginReadArray("logins")
for i in range(size):
settings.setArrayIndex(i)
login = Login()
login.userName = settings.value("userName")
login.password = settings.value("password")
logins.append(login)
settings.endArray()
使用 PySide.QtCore.QSettings.beginWriteArray() to write the array in the first place.
| 参数: |
|
|---|
添加 prefix to the current group and starts writing an array of size size 。若 size is -1 (the default), it is automatically determined based on the indexes of the entries written.
If you have many occurrences of a certain set of keys, you can use arrays to make your life easier. For example, let's suppose that you want to save a variable-length list of user names and passwords. You could then write:
class Login:
userName = ''
password = ''
logins = []
...
settings = QSettings()
settings.beginWriteArray("logins")
for i in range(logins.size()):
settings.setArrayIndex(i)
settings.setValue("userName", list.at(i).userName)
settings.setValue("password", list.at(i).password)
settings.endArray()
The generated keys will have the form
To read back an array, use PySide.QtCore.QSettings.beginReadArray() .
| 返回类型: | 字符串列表 |
|---|
Returns a list of all key top-level groups that contain keys that can be read using the PySide.QtCore.QSettings 对象。
范例:
settings = QSettings()
settings.setValue("fridge/color", Qt.white)
settings.setValue("fridge/size", QSize(32, 96));
settings.setValue("sofa", True)
settings.setValue("tv", False)
groups = settings.childGroups()
# group: ["fridge"]
若设置组使用 PySide.QtCore.QSettings.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 PySide.QtCore.QSettings.childKeys() and PySide.QtCore.QSettings.childGroups() recursively.
| 返回类型: | 字符串列表 |
|---|
返回所有顶层键的列表,可以被读取使用 PySide.QtCore.QSettings 对象。
范例:
settings = QSettings()
settings.setValue("fridge/color", Qt.white)
settings.setValue("fridge/size", QSize(32, 96))
settings.setValue("sofa", True)
settings.setValue("tv", False)
keys = settings.childKeys()
# keys: ["sofa", "tv"]
若设置组使用 PySide.QtCore.QSettings.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 PySide.QtCore.QSettings.childKeys() and PySide.QtCore.QSettings.childGroups() recursively.
移除首要位置中的所有条目,关联此 PySide.QtCore.QSettings 对象。
不移除回退位置条目。
若只希望移除的条目在当前 PySide.QtCore.QSettings.group() , use remove(“”) instead.
| 参数: | key – unicode |
|---|---|
| 返回类型: | PySide.QtCore.bool |
Returns true if there exists a setting called key ;否则返回 false。
若设置组使用 PySide.QtCore.QSettings.beginGroup() , key 被认为是相对于该组的。
Note that the Windows registry and INI files use case-insensitive keys, whereas the Carbon Preferences API on Mac OS X uses case-sensitive keys. To avoid portability problems, see the Section and Key 句法 规则。
| 返回类型: | PySide.QtCore.QSettings.Format |
|---|
返回用于存储设置的默认文件格式为 PySide.QtCore.QSettings ( PySide.QtCore.QObject *) 构造函数。如果未设置默认格式, QSettings.NativeFormat 被使用。
Closes the array that was started using PySide.QtCore.QSettings.beginReadArray() or PySide.QtCore.QSettings.beginWriteArray() .
Resets the group to what it was before the corresponding PySide.QtCore.QSettings.beginGroup() 调用。
范例:
settings.beginGroup("alpha")
# settings.group() == "alpha"
settings.beginGroup("beta")
# settings.group() == "alpha/beta"
settings.endGroup()
# settings.group() == "alpha"
settings.endGroup()
# settings.group() == ""
| 返回类型: | PySide.QtCore.bool |
|---|
Returns true if fallbacks are enabled; returns false otherwise.
默认情况下,回退是启用的。
| 返回类型: | unicode |
|---|
Returns the path where settings written using this PySide.QtCore.QSettings object are stored.
在 Windows,若格式为 QSettings.NativeFormat , the return value is a system registry path, not a file path.
| 返回类型: | PySide.QtCore.QSettings.Format |
|---|
返回用于存储设置的格式。
| 返回类型: | unicode |
|---|
返回当前组。
| 返回类型: | PySide.QtCore.QTextCodec |
|---|
Returns the codec that is used for accessing INI files. By default, no codec is used, so a null pointer is returned.
| 返回类型: | PySide.QtCore.bool |
|---|
Returns true if settings can be written using this PySide.QtCore.QSettings object; returns false otherwise.
One reason why PySide.QtCore.QSettings.isWritable() might return false is if PySide.QtCore.QSettings 操作只读文件。
警告
此函数并不完美可靠,因为文件权限可以随时改变。
| 返回类型: | unicode |
|---|
返回用于存储设置的组织名称。
| 参数: | key – unicode |
|---|
移除设置 key 和任何子设置为 key .
范例:
settings = QSettings()
settings.setValue("ape")
settings.setValue("monkey", 1)
settings.setValue("monkey/sea", 2)
settings.setValue("monkey/doe", 4)
settings.remove("monkey")
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 PySide.QtCore.QSettings.remove() .
若 key 为空字符串,所有键在当前 PySide.QtCore.QSettings.group() are removed. For example:
settings = QSettings()
settings.setValue("ape")
settings.setValue("monkey", 1)
settings.setValue("monkey/sea", 2)
settings.setValue("monkey/doe", 4)
settings.beginGroup("monkey")
settings.remove("")
settings.endGroup()
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 Section and Key 句法 规则。
| 返回类型: | PySide.QtCore.QSettings.Scope |
|---|
返回用于存储设置的作用域。
| 参数: | i – PySide.QtCore.int |
|---|
将当前数组索引设为 i 。调用函数,譬如 PySide.QtCore.QSettings.setValue() , PySide.QtCore.QSettings.value() , PySide.QtCore.QSettings.remove() ,和 PySide.QtCore.QSettings.contains() will operate on the array entry at that index.
必须调用 PySide.QtCore.QSettings.beginReadArray() or PySide.QtCore.QSettings.beginWriteArray() before you can call this function.
| 参数: | format – PySide.QtCore.QSettings.Format |
|---|
将默认文件格式设为给定 format ,用于存储设置为 PySide.QtCore.QSettings ( PySide.QtCore.QObject *) 构造函数。
如果未设置默认格式, QSettings.NativeFormat 被使用。见文档编制了解 PySide.QtCore.QSettings 构造函数,查看构造函数是否会忽略此函数。
| 参数: | b – PySide.QtCore.bool |
|---|
把是否启用回退设为 b .
默认情况下,回退是启用的。
| 参数: | codecName – str |
|---|
这是重载函数。
设置访问 INI 文件的编解码器 (包括 .conf 文件在 Unix) 到 PySide.QtCore.QTextCodec 为指定编码通过 codecName 。常见值对于 codecName include “ISO 8859-1”, “UTF-8”, and “UTF-16”. If the encoding isn't recognized, nothing happens.
| 参数: | codec – PySide.QtCore.QTextCodec |
|---|
设置访问 INI 文件的编解码器 (包括 .conf 文件在 Unix) 到 codec 。编解码器被用于解码从 INI 文件读取的任何数据,和对写入文件的任何数据进行编码。默认情况下,不使用编解码器,和使用标准 INI 转义序列编码非 ASCII 字符。
警告
之后必须立即设置编解码器当创建 PySide.QtCore.QSettings 对象,在访问任何数据之前。
| 参数: |
|
|---|
设置用于存储设置的路径为给定 format and scope ,到 path 。 format 可以是自定义格式。
下表汇总了默认值:
| 平台 | 格式 | 作用域 | Path |
| 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 | ||
| Symbian | NativeFormat , IniFormat | UserScope | c:/data/.config |
| SystemScope | <drive>/private/<uid> |
默认 UserScope paths on Unix and Mac OS X ( $HOME/.config 或 $HOME/Settings) 可以由用户覆盖通过设置 XDG_CONFIG_HOME 环境变量。默认 SystemScope paths on Unix and Mac OS X ( /etc/xdg ) 可以被覆盖当构建 Qt 库使用 configure 脚本的 --sysconfdir 标志 (见 PySide.QtCore.QLibraryInfo 了解细节)。
设置 NativeFormat paths on Windows and Mac OS X has no effect.
警告
此函数不影响现有 PySide.QtCore.QSettings 对象。
另请参阅
registerFormat()
| 参数: |
|
|---|
设置值为设置 key to value 。若 key 已存在,先前值被覆写。
Note that the Windows registry and INI files use case-insensitive keys, whereas the Carbon Preferences API on Mac OS X uses case-sensitive keys. To avoid portability problems, see the Section and Key 句法 规则。
范例:
settings = QSettings()
settings.setValue("interval", 30)
settings.value("interval") # returns 30
settings.setValue("interval", 6.55)
settings.value("interval") # returns 6.55
| 返回类型: | PySide.QtCore.QSettings.Status |
|---|
返回状态代码指示遇到的首个错误通过 PySide.QtCore.QSettings ,或 QSettings.NoError 若没有发生错误。
注意: PySide.QtCore.QSettings 延迟履行某些操作。出于此原因,可能想要调用 PySide.QtCore.QSettings.sync() to ensure that the data stored in PySide.QtCore.QSettings is written to disk before calling PySide.QtCore.QSettings.status() .
把任何未保存改变写入永久存储,并重新加载同时已被其它应用程序改变的任何设置。
此函数被自动调用从 PySide.QtCore.QSettings ‘s destructor and by the event loop at regular intervals, so you normally don't need to call it yourself.
| 参数: |
|
|---|---|
| 返回类型: |
object |
返回值为设置 key 。若设置不存在,返回 defaultValue .
若未指定默认值,默认 PySide.QtCore.QVariant 被返回。
Note that the Windows registry and INI files use case-insensitive keys, whereas the Carbon Preferences API on Mac OS X uses case-sensitive keys. To avoid portability problems, see the Section and Key 句法 规则。
范例:
settings = QSettings()
settings.setValue("animal/snake", 58)
settings.value("animal/snake", 1024) # returns 58
settings.value("animal/zebra", 1024) # returns 1024
settings.value("animal/zebra") # returns 0
另请参阅
PySide.QtCore.QSettings.setValue() PySide.QtCore.QSettings.contains() PySide.QtCore.QSettings.remove()
警告
QSettings.value can return different types (QVariant types) depending on the platform it's running on, so the safest way to use it is always casting the result to the desired type, e.g.: int(settings.value(“myKey”))