1、定义configfile函数为外部静态函数,方便其他直接文件引用。
2、配置文件初始化,先判断路径,没有创建,然后新建配置信息。
3、配置文件读取,写入采取QSetting分组,键值对的方式。
configfile.h
#ifndef CONFIGFILE_H #define CONFIGFILE_H #include "globe_data.h" #include "system_config.h" class ConfigFile { public: ConfigFile(); ~ConfigFile(); //----------配置文件----------// static int ConfigFile_Init(); static void ConfigFile_Read_SetUp(Globe_Data *g);//返回读取的个数 static void ConfigFile_Write(Globe_Data *g); }; #endif // CONFIGFILE_H
configfile.cpp
#include "configfile.h" #include <QDir> #include <QSettings> #include <QDebug> ConfigFile::ConfigFile() { } ConfigFile::~ConfigFile() { } int ConfigFile::ConfigFile_Init() { QDir parameterDir; QFile file; Globe_Data globeData; //数据配置文件路径创建**************** { //QDir::currentPath() if(parameterDir.exists(QString::fromUtf8(DIR_WORKSPACE)+ QString::fromUtf8(DIR_CONFIG) )) { qDebug("the file /Navigator_WorkSpace/DataStore/DataConfig is exists"); } else { qDebug("the file /Navigator_WorkSpace/DataStore/DataConfig is not exists !"); if(parameterDir.mkpath(QString::fromUtf8(DIR_WORKSPACE)+ QString::fromUtf8(DIR_CONFIG) ) ) { qDebug("the dir /Navigator_WorkSpace/DataStore/DataConfig is created!!"); } else { qCritical("/Navigator_WorkSpace/DataStore/DataConfig is falied to create !!"); } } } //数据配置文件夹创建**************** { // /Navigator_WorkSpace/DataStore/DataConfig/config.ini file.setFileName( QString::fromUtf8(DIR_WORKSPACE)+ QString::fromUtf8(DIR_CONFIG)+ QString::fromUtf8(FILE_CONFIG)); if(!file.open(QIODevice::ReadOnly)) { qDebug("配置文件不存在!准备创建!"); //----------写入数据库配置数据----------// QSettings set( QString::fromUtf8(DIR_WORKSPACE)+ QString::fromUtf8(DIR_CONFIG)+ QString::fromUtf8(FILE_CONFIG) , QSettings::IniFormat);//文件存在则打开,不存在则创建 set.setIniCodec("UTF-8"); set.beginGroup("SQL-Standard"); set.setValue("hostName","127.0.0.1"); set.setValue("databasePath","/Navigator_WorkSpace/HY_01/"); set.setValue("databaseName","/Navigator_WorkSpace/HY_01/HT_12.db"); set.setValue("username",""); set.setValue("password",""); set.setValue("databaseType","QSQLITE"); set.setValue("testOnBorrow",true); set.setValue("testOnBorrowSql","SELECT 1"); set.setValue("maxWaitTime",1000); set.setValue("waitInterval",200); set.setValue("maxConnectionCount",5); set.endGroup(); //----------写入SetUp界面数据----------// set.beginGroup("SetUp-AppConfig"); set.setValue("depth",1); set.setValue("timeDate",2); set.setValue("time",3);set.setValue("ID",28);set.endGroup(); file.close(); return -1; } else { qDebug()<<"找到配置文件,正在打开!"; return 0; } } } void ConfigFile::ConfigFile_Read_SetUp(Globe_Data *g) { QSettings set( QString::fromUtf8(DIR_WORKSPACE)+ QString::fromUtf8(DIR_CONFIG)+ QString::fromUtf8(FILE_CONFIG) , QSettings::IniFormat);//文件存在则打开,不存在则创建 set.setIniCodec("UTF-8"); set.beginGroup("SetUp-AppConfig"); g->configFileInfor.mutex.lock(); g->configFileInfor.configValue.str_depth = set.value("depth").toString(); g->configFileInfor.configValue.str_timeDate = set.value("timeDate").toString(); g->configFileInfor.configValue.str_time = set.value("time").toString(); g->configFileInfor.configValue.str_ID = set.value("ID").toString(); g->configFileInfor.mutex.unlock(); //qDebug()<<"进程所在目录="<<QDir::currentPath();进程工作位置 set.endGroup(); } void ConfigFile::ConfigFile_Write(Globe_Data *g) { QSettings set( QString::fromUtf8(DIR_WORKSPACE)+ QString::fromUtf8(DIR_CONFIG)+ QString::fromUtf8(FILE_CONFIG) , QSettings::IniFormat);//文件存在则打开,不存在则创建 set.setIniCodec("UTF-8"); set.beginGroup("SetUp-AppConfig"); g->configFileInfor.mutex.lock(); set.setValue("depth",g->configFileInfor.configValue.str_depth); set.setValue("timeDate",g->configFileInfor.configValue.str_timeDate); set.setValue("time",g->configFileInfor.configValue.str_time);set.setValue("ID",g->configFileInfor.configValue.str_ID); g->configFileInfor.mutex.unlock(); set.endGroup(); }