QSetting的构造函数原型:
复制代码
1
2
3
4
5
6QSettings::QSettings ( Format format, Scope scope, const QString & organization, const QString & application = QString(), QObject * parent = 0 )
enum QSettings::Format
这个枚举类型指定QSettings所使用的存储格式。
常量 | 值 | 描述 |
---|---|---|
QSettings::NativeFormat | 0 | 使用平台最合适的存储格式设置。在Windows中,使用系统注册表;OS X和iOS中,使用的是CFPreferences |
QSettings::IniFormat | 1 | 存储在INI文件中的设置。 |
QSettings::InvalidFormat | 16 | registerFormat()返回的值 |
Unix中,NativeFormat和IniFormat意思是一样的,只是文件扩展名不同(NativeFormat为.conf,IniFormat 为.ini)。
enum QSettings::Scope
该枚举指定设置是否用户特定或同一系统的所有用户共享。
常量 | 值 | 描述 |
---|---|---|
QSettings::UserScope | 0 | 在一个位置存储特定于当前用户的设置(例如,用户的主目录)。 |
QSettings::SystemScope | 1 | 在一个全局位置存储设置,以便在相同机器上所有用户访问同一组的设置。 |
void QSettings::setPath(Format format, Scope scope, const QString & path)
为给定格式和范围设置用来存储的路径。对于路径而言,该格式可以是自定义格式。
下表总结了默认值:
平台 | 格式 | 范围 | 路径 |
---|---|---|---|
Windows | IniFormat | 1.UserScope 2.SystemScope | 1.%APPDATA% 2.%COMMON_APPDATA% |
Unix | NativeFormat, IniFormat | 1.UserScope 2.SystemScope | 1.$HOME/.config 2./etc/xdg |
Qt for Embedded Linux | NativeFormat, IniFormat | 1.UserScope 2.SystemScope | 1.$HOME/Settings 2./etc/xdg |
OS X and iOS | IniFormat | 1.UserScope 2.SystemScope | 1.$HOME/.config 2./etc/xdg |
在Windows、OS X、ios中设置NativeFormat没有任何效果。
警告:此功能不会影响现有QSettings对象。
示例代码:
在window上,写ini配置文件:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <Qtcore/QSettings> int main(int argc, char *argv[]) { QSettings setting("Info.ini", QSettings::IniFormat); // setting.clear(); // 会将文件中遗留的数据全部清空 setting.beginGroup("UserInfo"); setting.setValue("username", "admin"); setting.setValue("password", "123456"); setting.endGroup(); return 0; }
写的Info.ini文件的数据如下:
读ini文件示例代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <QtCore/QSettings> #include <QtCore/QString> #include <QtCore/QStringList> #include <iostream> int main(int argc, char *argv[]) { QSettings setting("Info.ini", QSettings::IniFormat); QStringList groups = setting.childGroups(); for (int i = 0; i < groups.size(); ++i) { setting.beginGroup(groups[i]); std::cout << "[" << groups[i].toStdString() << "]n"; std::cout << "username=" << setting.value("username").toString().toStdString() << "n"; std::cout << "password=" << setting.value("password").toString().toStdString() << "n"; setting.endGroup(); } return 0; }
最后
以上就是开放刺猬最近收集整理的关于Qt笔记 -- QSetting的使用的全部内容,更多相关Qt笔记内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复