Skip to content

Commit 905a126

Browse files
添加json对象处理
1 parent b93284b commit 905a126

File tree

8 files changed

+498
-1
lines changed

8 files changed

+498
-1
lines changed

qt_http/qt_http.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
2424
# You can also select to disable deprecated APIs only up to a certain version of Qt.
2525
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
2626

27-
CONFIG += c++17
27+
CONFIG += c++17 object_parallel_to_source
2828

2929
SOURCES += \
3030
main.cpp \

qt_json/form_plant.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include "form_plant.h"
2+
3+
JsonFormPlant::JsonFormPlant()
4+
{
5+
6+
}
7+
8+
void JsonFormPlant::SetKey(const QString &str)
9+
{
10+
m_jsonFormInfo.key = str;
11+
}
12+
13+
void JsonFormPlant::SetType(const QString &str)
14+
{
15+
if (str == "int") {
16+
m_jsonFormInfo.type = JsonMemberTypeInt;
17+
}
18+
else if (str == "string") {
19+
m_jsonFormInfo.type = JsonMemberTypeStr;
20+
} else if (str == "bool") {
21+
m_jsonFormInfo.type = JsonMemberTypeBool;
22+
}
23+
}
24+
25+
void JsonFormPlant::SetValue(const QString &str)
26+
{
27+
m_jsonFormInfo.value = str;
28+
}
29+
30+
void JsonFormPlant::SetStepSize(int32_t step)
31+
{
32+
m_jsonFormInfo.step = step;
33+
}
34+
35+
JsonFormInfo JsonFormPlant::GetCurrentJsonForm()
36+
{
37+
return m_jsonFormInfo;
38+
}
39+
40+
void JsonFormPlant::AddJsonForm(JsonFormInfo &jsonFormInfo)
41+
{
42+
m_jsonFormPlantItems.emplace_back(jsonFormInfo);
43+
}
44+
45+
Json JsonFormPlant::GenerateFormJson(int32_t num)
46+
{
47+
Json ret;
48+
for (int32_t i = 0; i < num; i ++) {
49+
Json item;
50+
for (auto & jsonInfo : m_jsonFormPlantItems) {
51+
if (jsonInfo.type == JsonMemberTypeInt) {
52+
auto value = std::stoll(jsonInfo.value.toStdString());
53+
value += jsonInfo.diff;
54+
item[jsonInfo.key.toStdString()] = value;
55+
jsonInfo.diff += jsonInfo.step;
56+
} else if (jsonInfo.type == JsonMemberTypeStr) {
57+
item[jsonInfo.key.toStdString()] = jsonInfo.value.toStdString();
58+
} else if (jsonInfo.type == JsonMemberTypeBool) {
59+
if (jsonInfo.value == true) {
60+
item[jsonInfo.key.toStdString()] = true;
61+
} else {
62+
item[jsonInfo.key.toStdString()] = true;
63+
}
64+
}
65+
}
66+
67+
ret.push_back(item);
68+
}
69+
70+
for (auto & jsonInfo : m_jsonFormPlantItems) {
71+
if (jsonInfo.type == JsonMemberTypeInt) {
72+
jsonInfo.diff = 0;
73+
}
74+
}
75+
76+
77+
return ret;
78+
}
79+
80+
Json JsonFormPlant::PriviewJsonInfo()
81+
{
82+
Json item;
83+
for (auto & jsonInfo : m_jsonFormPlantItems) {
84+
if (jsonInfo.type == JsonMemberTypeInt) {
85+
auto value = std::stoll(jsonInfo.value.toStdString());
86+
value += jsonInfo.diff;
87+
item[jsonInfo.key.toStdString()] = value;
88+
} else if (jsonInfo.type == JsonMemberTypeStr) {
89+
item[jsonInfo.key.toStdString()] = jsonInfo.value.toStdString();
90+
} else if (jsonInfo.type == JsonMemberTypeBool) {
91+
if (jsonInfo.value == true) {
92+
item[jsonInfo.key.toStdString()] = true;
93+
} else {
94+
item[jsonInfo.key.toStdString()] = true;
95+
}
96+
}
97+
}
98+
return item;
99+
}

qt_json/form_plant.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef FORM_PLANT_H
2+
#define FORM_PLANT_H
3+
4+
#include <vector>
5+
#include <QString>
6+
7+
8+
#include "nlohmann/json.hpp"
9+
using Json = nlohmann::json;
10+
11+
12+
enum JsonMemberType {
13+
JsonMemberTypeInt = 1,
14+
JsonMemberTypeStr = 2,
15+
JsonMemberTypeBool = 3,
16+
};
17+
18+
struct JsonFormInfo
19+
{
20+
QString key;
21+
JsonMemberType type;
22+
QString value;
23+
int32_t step{0};
24+
int64_t diff{0}; // 总共差值
25+
};
26+
27+
28+
class JsonFormPlant
29+
{
30+
public:
31+
JsonFormPlant();
32+
33+
// key值
34+
void SetKey(const QString & str);
35+
// key值
36+
void SetType(const QString & str);
37+
// value 值
38+
void SetValue(const QString & str);
39+
// 步长
40+
void SetStepSize(int32_t step);
41+
42+
JsonFormInfo GetCurrentJsonForm();
43+
44+
void AddJsonForm(JsonFormInfo & jsonFormInfo);
45+
46+
Json GenerateFormJson(int32_t num);
47+
48+
Json PriviewJsonInfo();
49+
50+
private:
51+
JsonFormInfo m_jsonFormInfo;
52+
53+
std::vector<JsonFormInfo> m_jsonFormPlantItems;
54+
55+
};
56+
57+
#endif // FORM_PLANT_H

qt_json/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "mainwindow.h"
2+
#include <QApplication>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
QApplication a(argc, argv);
7+
MainWindow w;
8+
w.show();
9+
10+
return a.exec();
11+
}

qt_json/mainwindow.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "mainwindow.h"
2+
#include "ui_mainwindow.h"
3+
4+
#include <iostream>
5+
#include <QDebug>
6+
7+
MainWindow::MainWindow(QWidget *parent) :
8+
QMainWindow(parent),
9+
ui(new Ui::MainWindow)
10+
{
11+
ui->setupUi(this);
12+
setWindowTitle("Topic辅助工具");
13+
14+
m_lpJsoFormPlant = new JsonFormPlant;
15+
16+
connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::PushButtonAdd);
17+
connect(ui->pushButton_2, &QPushButton::clicked, this, &MainWindow::GenerateJson);
18+
19+
}
20+
21+
MainWindow::~MainWindow()
22+
{
23+
delete m_lpJsoFormPlant;
24+
delete ui;
25+
}
26+
27+
void MainWindow::PushButtonAdd()
28+
{
29+
m_lpJsoFormPlant->SetKey(ui->lineEdit->text());
30+
m_lpJsoFormPlant->SetType(ui->comboBox->currentText());
31+
m_lpJsoFormPlant->SetValue(ui->lineEdit_2->text());
32+
m_lpJsoFormPlant->SetStepSize(ui->spinBox_2->value());
33+
auto jsonInfo = m_lpJsoFormPlant->GetCurrentJsonForm();
34+
m_lpJsoFormPlant->AddJsonForm(jsonInfo);
35+
auto itemJson = m_lpJsoFormPlant->PriviewJsonInfo();
36+
std::string dumpStr = itemJson.dump(4);
37+
std::cout << dumpStr << std::endl;;
38+
ui->label->setText(QString::fromStdString(dumpStr));
39+
}
40+
41+
void MainWindow::GenerateJson()
42+
{
43+
44+
auto num = ui->spinBox->value();
45+
auto retJson = m_lpJsoFormPlant->GenerateFormJson(num);
46+
47+
std::string dumpStr = retJson.dump(4);
48+
49+
50+
ui->textEdit->setText(QString::fromStdString(dumpStr));
51+
52+
53+
54+
}

qt_json/mainwindow.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef MAINWINDOW_H
2+
#define MAINWINDOW_H
3+
4+
#include <QMainWindow>
5+
6+
#include "form_plant.h"
7+
8+
9+
namespace Ui {
10+
class MainWindow;
11+
}
12+
13+
class MainWindow : public QMainWindow
14+
{
15+
Q_OBJECT
16+
17+
public:
18+
explicit MainWindow(QWidget *parent = nullptr);
19+
~MainWindow();
20+
21+
22+
void PushButtonAdd();
23+
24+
void GenerateJson();
25+
26+
private:
27+
Ui::MainWindow *ui;
28+
29+
JsonFormPlant *m_lpJsoFormPlant;
30+
};
31+
32+
#endif // MAINWINDOW_H

0 commit comments

Comments
 (0)