Linux系统中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,例如linux系统。

QSettings::Scope(配置存储范围):

分为UserScope、SystemScope。
QSettings::UserScope:用户环境,设置在当前用户的特定位置中。
QSettings::SystemScope:系统环境,设置在全局型,所有用户均可获得。

我们创建一个Qsettings的对象时,我们需要传递给它两个参数,第一个是你公司或者组织的名称,第二个事你的应用程序的名称。比如:

    Settings = Qsettings(“MySoft”,”QtPad”)

   公司名称:MySoft,程序名称:QtPad

以下是对应QSettings::Format和QSettings::Scope存放的默认路径位置,在Unix/X11平台下这两个Format效果是一样的,其中*表示的是对应的程序名称:

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

本文主要是根据自己win7应用软件移植到linux时,QSettings的使用未曾更改,但是依然可以正常跑起来,然后告诉大家linux虽然没有注册表,但是依然可以正常保存,存到文件中。

转自:https://www.cnblogs.com/findumars/p/6250752.html?utm_source=itdadao&utm_medium=referral

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

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

发布了27 篇原创文章 · 获赞 25 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/weixin_38293850/article/details/99966431