QSetting读写注册表

来源:互联网 发布:知豆可以跑多少公里 编辑:程序博客网 时间:2024/06/06 02:12

        注册表是windows操作系统中的一个核心数据库,其中存放着各种参数,直接控制着windows的启动、硬件驱动程序装载以及一些windows应用程序的运行,从而在整个系统中起着核心作用。一个应用程序读取注册表就可以实现将自己程序的配置信息放到本地,这样程序初始化时候就可以获取这些本地信息恢复默认设置。

     在Qt中相关功能由QSetting完成实现。Qt文档中相关描述:

The QSettings class provides persistent platform-independent application settings.
Users normally expect an application to remember its settings (window sizes and positions, options, etc.) across sessions. This information is often stored in the system registry on Windows, and in property list files on OS X and iOS. On Unix systems, in the absence of a standard, many applications (including the KDE applications) use INI text files.
QSettings is an abstraction around these technologies, enabling you to save and restore application settings in a portable manner. It also supports custom storage formats.
QSettings's API is based on QVariant, allowing you to save most value-based types, such as QString, QRect, and QImage, with the minimum of effort.
If all you need is a non-persistent memory-based structure, consider using QMap<QString, QVariant> instead.

其提供了一个与平台无关应用程序设置。使用QSettting 我们需要先初始化:

QSettings settings("SegSystem","Segmentation");
    第一个参数:是组织名   第二个参数是你的应用程序名。

设置存储信息:

QSetting通过setvalue函数设置存储在注册表中的信息。存储形式:键值对形式(key-value)默认键值对类型是<QString,QVariant>

  key有两种写法:1 使用路径形式的语法

settings.setValue("FileMenu/recentFiles",recentFiles);

                   2使用begingroup和endgroup设置分组

settings.beginGroup("FileMenu");    settings.setValue("recentFiles",recentFiles);    settings.endGroup();
两种方式再注册表中形成的一样的树目录结构



读取注册表中的存储信息

      读取信息 使用value 函数 因为返回值是QVariant所以要类型转换

 settings.beginGroup("FileMenu");    recentFiles=settings.value("recentFiles").toStringList();    updateRecentFile();    settings.endGroup();
注意:读取信息要按照程序在注册表中的树目录结构来读。

关于注册表的权限问题

当我们项注册表写入时会涉及到修改注册表的管理者权限问题。一个解决办法如下。 
         1、以“管理员权限”运行qt creator。在Creator中运行编译后的exe文件,成功的修改了注册表。 
         2、 realese时,先生成exe文件。再使用win SDK的mt.exe工具把修改后的manifest打包到exe文件中。这时exe文件会被加上一个盾牌的图标,win7启动它时,会自动弹                  出确认框,让用户授予管理员权限。 



0 0
原创粉丝点击