Qt软件开发文档18---QSettings类的封装与调用,文件路径判断
来源:互联网 发布:洛天依软件如何解压 编辑:程序博客网 时间:2024/05/22 14:30
简述
软件开发时候,往往需要利用QSettings写入ini配置文件
多次调用QSettings的读写加大代码量,同时,修改时也会加大难度。
为此封装一个ConFRouter类,利用QSettings统一管理配置文件里的内容。
类的声明与定义
1. ConFRouter.h
class DLL_API ConFRouter{public: static ConFRouter* create_confrouter(const QString& path = QString("")); void save_file(); void read_file(); QString& operator[](const QString& key);private: ConFRouter(const QString& path); QString _root; QHash<QString, QString*> _find; QString _case_path; QString _screen_shot_path; QString _video_path; QString _display_value; std::vector<QString> _groups = { "CaseGroup", "PathGroup" }; std::vector<vector<QString>> _keynames = { { "case_path" }, { "video_path", "screen_shot_path", "display_value" } };};
2. ConFRouter.cpp
ConFRouter* ConFRouter::create_confrouter(const QString& path){ static ConFRouter* con_pointer = nullptr; if (con_pointer == nullptr) { assert(path.size() != 0); con_pointer = new ConFRouter(path); } return con_pointer;}ConFRouter::ConFRouter(const QString& path){ auto make_hashs = [](const std::vector<vector<QString>>& keynames, const std::vector<vector<QString*>>& pointers) { QHash<QString, QString*> hashs; assert(keynames.size() == pointers.size()); for (size_t i = 0; i < keynames.size(); i++) { assert(keynames[i].size() == pointers[i].size()); for (size_t j = 0; j < keynames[i].size(); j++) hashs.insert(keynames[i][j], pointers[i][j]); } return hashs; }; auto set_defult = [=](const std::vector<vector<QString*>>& pointers, const std::vector<vector<QString>> defaults) { assert(defaults.size() == pointers.size()); for (size_t i = 0; i < pointers.size(); i++) { assert(defaults[i].size() == pointers[i].size()); for (size_t j = 0; j < pointers[i].size(); j++) *(pointers[i][j]) = defaults[i][j]; } }; std::vector<vector<QString>> defaults = { { path + "/vinci_case" }, { path + "/avi", path + "/screenshot", "0.03" } }; std::vector<vector<QString*>> pointers = { { &_case_path }, { &_video_path, &_screen_shot_path, &_display_value } }; _root = path; _find = make_hashs(_keynames, pointers); set_defult(pointers, defaults); this->read_file();}void ConFRouter::save_file(){ std::shared_ptr<QSettings> con = std::shared_ptr<QSettings>( new QSettings(_root + "/settings.ini", QSettings::IniFormat), [](QSettings *setting) { delete setting; } ); for (size_t i = 0; i < _groups.size(); i++) { con->beginGroup(_groups[i]); for (size_t j = 0; j < _keynames[i].size(); j++) con->setValue(_keynames[i][j], (*this)[_keynames[i][j]]); con->endGroup(); } con->sync();}void ConFRouter::read_file(){ std::shared_ptr<QSettings> con = std::shared_ptr<QSettings>( new QSettings(_root + "/settings.ini", QSettings::IniFormat), [](QSettings *setting) { delete setting; } ); for (size_t i = 0; i < _groups.size(); i++) { con->beginGroup(_groups[i]); for (size_t j = 0; j < _keynames[i].size(); j++) { QString value = con->value(_keynames[i][j]).toString(); if (value.size() != 0) (*this)[_keynames[i][j]] = value; } con->endGroup(); }}QString & ConFRouter::operator[](const QString & key){ auto pointer = this->_find.find(key); assert(pointer != this->_find.end()); return *(pointer.value());}
类的调用
1.mainWnd中声明Path
在创建mainWnd时候,在对应得appPath下创建setting.ini
#include "ConFRouter.h"...ConFRouter::create_confrouter(QApplication::applicationDirPath());
2.读取setting.ini
读取的信息为QString类型,需要我们自己对类型转换。
void CSettingDlg::read_iniFile(){ auto con = ConFRouter::create_confrouter(); QString screenShotPath = (*con)["screen_shot_path"]; QString videoPath = (*con)["video_path"]; QString displayValue = (*con)["display_value"]; if ((checkFloderPath(screenShotPath) == FloderPath::INVALID)) { (*con)["screen_shot_path"] = this->_appPath; screenShotPath = this->_appPath; } if ((checkFloderPath(videoPath) == FloderPath::INVALID)) { (*con)["video_path"] = this->_appPath; videoPath = this->_appPath; } bool ok; displayValue.toDouble(&ok); if (ok == false) { (*con)["display_value"] = 0.3; displayValue = 0.3; } con->save_file(); this->_screen_shot_edit->setText(screenShotPath); this->_video_edit->setText(videoPath); this->_display_box->setValue(displayValue.toDouble());}
3.修改setting.ini的内容
void CSettingDlg::write_iniFile(){ auto con = ConFRouter::create_confrouter(); QString screenShotPath = this->_screen_shot_edit->text(); QString videoPath = this->_video_edit->text(); double displayValue = this->_display_box->value(); (*con)["screen_shot_path"] = screenShotPath; (*con)["video_path"] = videoPath; (*con)["display_value"] = QString("%1").arg(displayValue); con->save_file();}
对路径的判断
读取到的路径,需要写入一个func判断路径是否合理。
int CSettingDlg::checkFloderPath(const QString path){ if (0 == path.size()) return FloderPath::INVALID; QStringList allDirs; if (path.contains("/")) allDirs = path.split("/", QString::SkipEmptyParts); else allDirs = path.split("\\", QString::SkipEmptyParts); QString dirPath = allDirs.at(0); QFileInfo rootPath(dirPath + "/"); if (false == rootPath.isRoot()) return FloderPath::INVALID; QDir dir; for (size_t i = 1; i < allDirs.size(); i++) { dirPath = dirPath + "/" + allDirs.at(i); if (false == dir.exists(dirPath)) { if (false == dir.mkdir(dirPath)) return FloderPath::INVALID; } } return FloderPath::VALID;}
阅读全文
3 0
- Qt软件开发文档18---QSettings类的封装与调用,文件路径判断
- Qt软件开发文档9---封装类调用
- 嵌入式软件开发------------------Qt之QSettings类的常用函数
- Qt 类QSettings,读取ini与注册表
- Qt软件开发文档8---登陆界面的绘制及其封装
- Qt软件开发文档3---mainAPP调用dllManager中函数
- Qt之QSettings类详解
- QT ini配置文件的读写(使用QSettings类)
- QT中QSettings类的对象创建方式
- QT QSettings读取文件中文乱码
- Qt配置信息设置(QSettings在不同平台下的使用路径)
- QSettings的用法详解(读Qt creator帮助文档自己理解 )
- qt QSettings
- Qt--QSettings
- 软件开发与文档
- 软件开发的文档与审查
- Qt软件开发文档4---主窗口的实现
- Qt软件开发文档5---父指针传递的妙用
- Android中内存优化相关知识点
- 《标题党》自我修炼的10个秘籍
- iOS引导页
- Unity 水波纹Shader
- BluetoothAdapter在Android6.0/7.0+以上startDiscovery不能发现蓝牙设备问题
- Qt软件开发文档18---QSettings类的封装与调用,文件路径判断
- String StringBuild Imoc
- B树和红黑数的区别
- Spring+ActiveMQ消息持久化,Topic持久化订阅
- armv8 不支持的指令造成的异常会在kernel中通过其他指令替换
- 分页显示时传递页码的方法
- iOS中的静态库与动态库,区别、制作和使用
- java学习环境变量详细配置方法
- 4、js基础:创建对象