添加配置文件
This commit is contained in:
+108
-4
@@ -15,7 +15,15 @@ Config::Config(QObject *parent) :
|
||||
delete Dir;
|
||||
Dir = nullptr;
|
||||
|
||||
loadJson("./config/config.json");
|
||||
loadJson(ConfigFile);
|
||||
|
||||
setConnet("dlink",
|
||||
"COM1","serialport",
|
||||
true,
|
||||
1,2,
|
||||
3,
|
||||
4,5);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -49,21 +57,116 @@ void Config::loadJson(const QString& jsonFilename)
|
||||
return;
|
||||
}
|
||||
|
||||
ConfigJson = doc.object();
|
||||
|
||||
/*
|
||||
QJsonObject json = doc.object();
|
||||
|
||||
QJsonValue jsonValue = json.value("config");
|
||||
|
||||
if(jsonValue.isObject())
|
||||
{
|
||||
ConfigJson = jsonValue.toObject();
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
QString Config::getDefaultCommandFile()
|
||||
QJsonObject Config::getJsonObject(QString key)
|
||||
{
|
||||
return "01";
|
||||
QJsonObject obj;
|
||||
|
||||
QJsonValue Value = ConfigJson.value(key);
|
||||
|
||||
if(Value.isObject())
|
||||
{
|
||||
obj = Value.toObject();
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
void Config::setDefaultCommandFile(QString file)
|
||||
void Config::setJsonObject(QString key,QJsonValue value)
|
||||
{
|
||||
|
||||
ConfigJson[key] = value;
|
||||
|
||||
|
||||
|
||||
QFile file(ConfigFile);
|
||||
if(!file.open(QIODevice::ReadWrite|QFile::Truncate)) {
|
||||
qDebug() << "File open error";
|
||||
}
|
||||
|
||||
|
||||
QJsonDocument doc(QJsonDocument::fromJson(file.readAll()));
|
||||
doc.setObject(ConfigJson);
|
||||
|
||||
|
||||
file.write(doc.toJson());
|
||||
file.close();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
QString Config::getCommandFile()
|
||||
{
|
||||
QJsonObject obj = getJsonObject("commandfile");
|
||||
|
||||
QString file = obj.value("path").toString();
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
void Config::setCommandFile(QString file)
|
||||
{
|
||||
QJsonObject obj;
|
||||
obj["path"] = file;
|
||||
|
||||
setJsonObject("commandfile",obj);
|
||||
}
|
||||
|
||||
void Config::getConnet(QString key,
|
||||
QVariant *name,QVariant *type,
|
||||
QVariant *iauto,
|
||||
QVariant *par1,QVariant *par2,
|
||||
QVariant *par3,QVariant *par4,
|
||||
QVariant *par5)
|
||||
{
|
||||
QJsonObject obj = getJsonObject(key);
|
||||
|
||||
*name = obj.value("name").toString();
|
||||
*type = obj.value("type").toString();
|
||||
*iauto = obj.value("auto").toString();
|
||||
*par1 = obj.value("param1").toString();
|
||||
*par2 = obj.value("param2").toString();
|
||||
*par3 = obj.value("param3").toString();
|
||||
*par4 = obj.value("param4").toString();
|
||||
*par5 = obj.value("param5").toString();
|
||||
}
|
||||
|
||||
void Config::setConnet(QString key,
|
||||
QVariant name,QVariant type,
|
||||
QVariant iauto,
|
||||
QVariant par1,QVariant par2,
|
||||
QVariant par3,QVariant par4,
|
||||
QVariant par5)
|
||||
{
|
||||
|
||||
QJsonObject obj;
|
||||
|
||||
obj["name"] = name.toString();
|
||||
obj["type"] = type.toString();
|
||||
obj["auto"] = iauto.toString();
|
||||
obj["param1"] = par1.toString();
|
||||
obj["param2"] = par2.toString();
|
||||
obj["param3"] = par3.toString();
|
||||
obj["param4"] = par4.toString();
|
||||
obj["param5"] = par5.toString();
|
||||
|
||||
setJsonObject(key,obj);
|
||||
}
|
||||
|
||||
|
||||
@@ -73,3 +176,4 @@ void Config::setDefaultCommandFile(QString file)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+27
-2
@@ -18,8 +18,30 @@ public:
|
||||
~Config();
|
||||
|
||||
|
||||
QString getDefaultCommandFile();
|
||||
void setDefaultCommandFile(QString file);
|
||||
|
||||
|
||||
public slots:
|
||||
QJsonObject getJsonObject(QString key);
|
||||
void setJsonObject(QString key,QJsonValue value);
|
||||
|
||||
|
||||
QString getCommandFile();
|
||||
void setCommandFile(QString file);
|
||||
|
||||
|
||||
void getConnet(QString key,
|
||||
QVariant *name, QVariant *type,
|
||||
QVariant *iauto,
|
||||
QVariant *par1, QVariant *par2,
|
||||
QVariant *par3, QVariant *par4,
|
||||
QVariant *par5);
|
||||
|
||||
void setConnet(QString key,
|
||||
QVariant name,QVariant type,
|
||||
QVariant iauto,
|
||||
QVariant par1,QVariant par2,
|
||||
QVariant par3,QVariant par4,
|
||||
QVariant par5);
|
||||
|
||||
|
||||
private slots:
|
||||
@@ -30,6 +52,9 @@ private slots:
|
||||
|
||||
private:
|
||||
|
||||
QString ConfigFile = "./config/config.json";
|
||||
|
||||
QJsonObject ConfigJson;
|
||||
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user