QSetting读写注册表
2016-05-19 16:56
411 查看
注册表是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启动它时,会自动弹 出确认框,让用户授予管理员权限。
在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启动它时,会自动弹 出确认框,让用户授予管理员权限。
相关文章推荐
- Linux驱动之设备模型(7)
- unix常用命令
- 10036---Java多线程学习(吐血超详细总结)
- 融云在网络切换的情况,怎么刷新列表
- 提升你的Java应用性能:改善数据处理
- 主动开释池是什么如何劳动
- XML类图简单回顾
- protoc 和 protoc-gen-go 产生的 proto 文件代码对比
- mysql 连接多行 合并多行
- HorizontalScrollView
- 高空作业安全规程要求
- Zabbix 小排错
- JavaScript笛卡尔积超简单的实现
- 浅析Android中的消息机制
- Linux驱动之设备模型(6)
- 不简单的单例模式Singleton
- elk之压测
- 使用tempest测试openstack
- 动态测试
- Android打开各种文件