一、QSettings

​ QT中,对.ini文件和注册表都是使用同一个类,就是QSettings类。使用简单,但是不知道是设置问题还是啥,会有点小问题,下面我会指出。

​ API中介绍,创建QSettings时,必须需要指定公司或者组织、应用名称。注册表使用的,读取INI不需要设置。(you must pass the name of your company or organization as well as the name of your application.)

1
QSettings settings("MySoft", "Star Runner");

1)简单介绍

函数名 作用
QSettings(const QString & fileName, Format format, QObject * parent = 0);

/
Format
QSettings::NativeFormat 0 在windows平台,用于读取注册表,OSX和IOS 读CFPreferences,Unix读INI
QSettings::IniFormat 1 Store the settings in INI files.
QSettings::InvalidFormat 16 Special value returned by registerFormat().
/ | 构造函数 |
| QStringList allKeys() const; | 返回所有的键 如group/key |
| void beginGroup(const QString & prefix); | 直接读取prefix组 |
| void QSettings::endGroup();
/
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();
/ | 返回上一级 |
| QStringList QSettings::childGroups() const; | 返回所有的group |
| QStringList QSettings::childKeys() const; | 返回当前级别的key |
| bool QSettings::contains(const QString & key) const; | 是否包含key |
| void QSettings::remove(const QString & key); | 删除本项及子项 |
| void QSettings::setIniCodec(QTextCodec codec); | 设置编码 |
| void QSettings::setIniCodec(const char
codecName); | |
| [static] void QSettings::setPath(Format format, Scope scope, const QString & path);
QSettings::UserScope 0 特定用户范围.
QSettings::SystemScope 1 本机所有用户. | 设置文件路径 |
| void QSettings::setValue(const QString & key, const QVariant & value); | 写入值 |
| QVariant QSettings::value(const QString & key, const QVariant & defaultValue = QVariant()) const; | 取值 |

2)读写配置文件

1
2
3
4
5
QSetting qset("./my.ini",QSettings::IniFormat);
qset.setValue("group/key","nice");
qDebug()<<qset.value("group/key"); //可以取到nice
//对于中文utf8的文档 需要增加一句
qset.setIniCodec("UTF8"); //GBK UNICODE GB2312

3)读写注册表

1
2
3
4
5
QSetting REGRead("MySoft", "Star Runner");	//会在注册表中建了一个MySoft,底下会有个Star Runner的文件夹 下面的操作会在 Star Runner中进行
//写
REGRead.setValue("","");
//读
REGRead.value("");

二、API

1)读写配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//读写系统配置文件
GetProfileInt(LPCWSTR lpAppName, LPCWSTR lpKeyName, INT nDefault);
GetProfileString(LPCSTR lpAppName, LPCSTR lpKeyName, LPCSTR lpDefault, LPSTR lpReturnedString, DWORD nSize);
WriteProfileString(LPCWSTR lpAppName, LPCWSTR lpKeyName, LPCWSTR lpString);
GetProfileSection(LPCWSTR lpAppName, LPWSTR lpReturnedString, DWORD nSize);
WriteProfileSection(LPCWSTR lpAppName, LPCWSTR lpString);
//读写自定义配置文件
GetPrivateProfileInt(LPCSTR lpAppName, LPCSTR lpKeyName, INT nDefault, LPCSTR lpFileName);
GetPrivateProfileString(LPCSTR lpAppName, LPCSTR lpKeyName, LPCSTR lpDefault, LPSTR lpReturnedString, DWORD nSize, LPCSTR lpFileName);
WritePrivateProfileString(LPCSTR lpAppName, LPCSTR lpKeyName, LPCSTR lpString, LPCSTR lpFileName);
GetPrivateProfileSection(LPCSTR lpAppName, LPSTR lpReturnedString, DWORD nSize, LPCSTR lpFileName);
WritePrivateProfileSection(LPCSTR lpAppName, LPCSTR lpString, LPCSTR lpFileName);
GetPrivateProfileSectionNames(LPWSTR lpszReturnBuffer, DWORD nSize, LPCWSTR lpFileName);
GetPrivateProfileStruct(LPCWSTR lpszSection, LPCWSTR lpszKey, LPVOID lpStruct, UINT uSizeStruct, LPCWSTR szFile);
WritePrivateProfileStruct(LPCWSTR lpszSection, LPCWSTR lpszKey, LPVOID lpStruct, UINT uSizeStruct, LPCWSTR szFile);

//例子
//写
WritePrivateProfileString(
reinterpret_cast<const wchar_t *>(group.utf16()),
reinterpret_cast<const wchar_t *>(key.utf16()),
reinterpret_cast<const wchar_t *>(error.utf16()),
reinterpret_cast<const wchar_t *>(filepath.utf16())
);
//读
wchar_t buf[64];
GetPrivateProfileString(
reinterpret_cast<const wchar_t *>(group.utf16()),
reinterpret_cast<const wchar_t *>(key.utf16()),
reinterpret_cast<const wchar_t *>(error.utf16()),
buf,sizeof(buf)-1,
reinterpret_cast<const wchar_t *>(filepath.utf16())
);
QString::fromWCharArray(buf);

2)读写注册表

​ 只捉几个介绍!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
RegCreateKeyEx(HKEY hKey,LPCWSTR lpSubKey,DWORD Reserved,LPWSTR lpClass,DWORD dwOptions,REGSAM samDesired,LPSECURITY_ATTRIBUTES lpSecurityAttributes,PHKEY phkResult,LPDWORD lpdwDisposition);
RegCreateKey(HKEY hKey,LPCWSTR lpSubKey,PHKEY phkResult);
/*
创建键值
*/
RegDeleteKeyEx(HKEY hKey,LPCWSTR lpSubKey,REGSAM samDesired,DWORD Reserved);
RegDeleteKey(HKEY hKey,LPCWSTR lpSubKey);
/*
删除键值
*/
RegOpenKeyEx(HKEY hKey,LPCWSTR lpSubKey,DWORD ulOptions,REGSAM samDesired,PHKEY phkResult);
RegOpenKey(HKEY hKey,LPCWSTR lpSubKey,PHKEY phkResult);
/*
打开键值
*/
RegQueryValueEx(HKEY hKey,LPCWSTR lpValueName,LPDWORD lpReserved,LPDWORD lpType,LPBYTE lpData,LPDWORD lpcbData);
RegQueryValue(HKEY hKey,LPCWSTR lpSubKey,LPWSTR lpData,PLONG lpcbData);
/*
获取键值
*/
RegSetValueEx(HKEY hKey,LPCWSTR lpValueName,DWORD Reserved,DWORD dwType,CONST BYTE *lpData,DWORD cbData);
RegSetValue(HKEY hKey,LPCWSTR lpSubKey,DWORD dwType,LPCWSTR lpData,DWORD cbData);
/*
修改键值
*/