在Windows操作系统中,⼤多把配置⽂件信息写在注册表当中,或写在*.ini⽂件中,对于这两种操作都有相应的Windows API函数,在以前的⽂章中都提及过,这⾥就不多说了~
在Qt中,提供了⼀个相应的配置⽂件的类QSetting,使⽤QSetting类,可以将⽤户设置以及应⽤程序的设置轻松存储在磁盘中。
QSettings::Format(配置存储格式)分为NativeFormat、IniFormat、InvalidFormat。这⾥主要讲的是NativeFormat和IniFormat。
QSettings::NativeFormat:在Windows中,利⽤系统注册表来存储;在 Mac OS X中,使⽤系统的CFPreferences机制来存储(使⽤Core Foundation Preference API);在其他平台中,设置则存储在⽂本⽂件中。
QSettings::IniFormat:读写*.ini格式的配置⽂件,NativeFormat在某些操作系统中的扩展名是*.conf。QSettings::Scope(配置存储范围)分为UserScope、SystemScope。QSettings::UserScope:⽤户环境,设置在当前⽤户的特定位置中。
QSettings::SystemScope:系统环境,设置在全局型,所有⽤户均可获得。
以下是对应QSettings::Format和QSettings::Scope存放的默认路径位置,其中*表⽰的是对应的程序名称:
Platform Format Scope Path
Windows NativeFormat UserScope HKEY_CURRENT_USER\\Software\\* SystemScope HKEY_LOCAL_MACHINE\\Software\\* IniFormat UserScope %APPDATA%\\*.ini
SystemScope %COMMON_APPDATA%\\*.iniUnix NativeFormat UserScope $HOME/.config/*.conf SystemScope /etc/xdg/*.conf
IniFormat UserScope $HOME/.config/*.ini SystemScope /etc/xdg/*.ini
Mac OS X NativeFormat UserScope $HOME/Library/Preferences/com.*.plist SystemScope /Library/Preferences/com.*.plist IniFormat UserScope $HOME/.config/*.ini SystemScope /etc/xdg/*.ini
在读写时,路径名必须是\"/\"⽽不是\"\\\\\"等。否则不能读写,注意。以Windows XP平台为例,举俩个例⼦程序~■、读写注册表
//Format为QSettings::NativeFormat
QSettings settings(\"HKEY_CURRENT_USER\\\\Software\\\\Microsoft\\\\Office\ QSettings::NativeFormat);//设置键值信息
settings.setValue(\"11.0/Outlook/Security/DontTrustInstalledFiles\//获取键值信息
int value = settings.value(\"11.0/Outlook/Security/DontTrustInstalledFiles\").toInt();
对应的值可是bool,double,QString,QStringList,或者是其他QVariant⽀持的数据类型,也包括注册过的⽤户⾃定义类型。删除设置对应的是settings->remove( const QString & key );■、读取ini配置⽂件
先定义下software.ini⽂件的格式,⽐较简单:[bolg]
Name = \"vic.MINg\"
//Format为QSettings::IniFormat
QSettings *setIni=new QSettings (\"software\//设置键值信息
setIni->beginGroup(\"bolg\");
setIni->setValue(\"Name\setIni->endGroup(); //获取键值信息
setIni->beginGroup(\"bolg\");
QString resault = setIni->value(\"Name\").toString(); setIni->endGroup(); qDebug()< 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- sceh.cn 版权所有 湘ICP备2023017654号-4
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务