Qt配置信息设置(QSettings在不同平台下的使用路径)

在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%\*.ini
Unix              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", 0);
//获取键值信息
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", QSettings::IniFormat); 
//设置键值信息
setIni->beginGroup("bolg"); 
setIni->setValue("Name", "vic.MINg"); 
setIni->endGroup(); 
//获取键值信息
setIni->beginGroup("bolg"); 
QString resault = setIni->value("Name").toString(); 
setIni->endGroup(); 
qDebug()<<resault;

QSetting的应用并不难,但很有用,一些更细致的操作用法,可以参看帮助文档!

可以参照示例:$QTDIR\examples\tools\settingseditor

http://cool.worm.blog.163.com/blog/static/643390062008426102655150/

 

posted @   findumars  Views(8355)  Comments(0Edit  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2016-01-05 utf8格式源代码中的字符串,默认都会当作char来处理,除非用L""符号来修饰
2016-01-05 Qt Installer Framework的学习
2016-01-05 发布Qt Quick桌面应用程序的方法
2015-01-05 MFC、WTL、WPF、wxWidgets、Qt、GTK、Cocoa、VCL 各有什么特点?
2015-01-05 C++Builder和VC的比较
2015-01-05 Apollo,Python,Delphi与Oracle之间的神话关系
2015-01-05 Delphi语言获得生命的原因和过程
点击右上角即可分享
微信分享提示