该文档将QSettings::clear
功能描述为:
删除与此 QSettings 对象关联的主要位置中的所有条目。
后备位置中的条目不会被删除。
但是,这是什么意思?主要位置和后备位置是什么???
主要位置取决于操作系统和您的设置。对于 Windows,这是注册表等。来自以下文档QSettings
:
假设您创建了一个 QSettings 对象,其组织名称为 MySoft,应用程序名称为 Star Runner。查找值时,最多按该顺序搜索四个位置:
- Star Runner 应用程序的用户特定位置
- MySoft 所有应用程序的用户特定位置
- Star Runner 应用程序的系统范围位置
- MySoft 为所有应用程序提供系统范围的位置
主要位置是最具体的位置:通常是您的应用程序的用户特定位置。
您可以为所有用户/应用程序提供共享默认值。但是,如果您调用它们,它们不会被删除clear()
。仅清除用户和应用程序特定的值。
如果您QSettings
使用公司和应用程序名称或使用默认构造函数初始化对象,则主要值是应用程序和用户特定的值。大多数应用程序都是这种情况。QSettings
如果您只是使用默认构造函数创建对象,则使用来自QApplication
(应用程序名称和组织名称)的值。
QSettings settings("MySoft", "Star Runner");
settings.clear();
// or
QSettings settings(); // use the values from QApplication
settings.clear();
如果你QSettings
用其他值初始化对象,你可以选择另一个主“存储”:
QSettings settings("MySoft");
settings.clear(); // clears values for whole company if possible.
QSettings settings(QSettings::SystemScope, "MySoft", "Star Runner");
settings.clear(); // clears system wide settings for the application.
QSettings settings(QSettings::SystemScope, "MySoft");
settings.clear(); // clears system wide settings for the company.
最后三种情况很少见,没有多大意义。此外,应用程序需要写入系统范围设置的权限。