-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberToJson.cpp
73 lines (61 loc) · 1.73 KB
/
NumberToJson.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "NumberToJson.h"
#include <QTextStream>
#include "ToJsonThread.h"
NumberToJson::NumberToJson(QObject *parent):
QObject(parent)
{
_fileName = "item.json";
}
void NumberToJson::start(int myInt)
{
QMap<int, QThread *> *allThread = new QMap<int, QThread *>;
countThread = myInt / 1000;
char myChar = 'a';
QFile file(_fileName);
if (file.open(QIODevice::Text | QIODevice::WriteOnly))
{
QTextStream out(&file);
out << "[\n";
file.close();
}
for (int i = 1; i <= countThread; ++i)
{
QThread *mThread = new QThread;
ToJsonThread *task = new ToJsonThread;
task->moveToThread(mThread);
connect(mThread, &QThread::started, task, &ToJsonThread::run1);
connect(mThread, &QThread::finished, task, &ToJsonThread::deleteLater);
connect(task, &ToJsonThread::goCompleteJson, this, &NumberToJson::CompleteJson);
QChar qChar = myChar;
task->setThreadID(QString(qChar));
task->setFileName(_fileName);
mThread->start();
allThread->insert(myChar, mThread);
++myChar;
}
}
void NumberToJson::CompleteJson()
{
getItemCounter++;
qInfo() << QString::number(getItemCounter) + " " + QString::number(countThread);
if (getItemCounter == countThread)
{
QFile file(_fileName);
QString jsonString;
if (file.open(QIODevice::Text | QIODevice::ReadOnly))
{
QTextStream out(&file);
jsonString = out.readAll();
file.close();
}
jsonString.remove(jsonString.lastIndexOf(','), jsonString.size());
if (file.open(QIODevice::Text | QIODevice::WriteOnly))
{
QTextStream out(&file);
out << jsonString;
out << ']';
file.close();
}
qInfo() << "NumberToJson completed!! (check:{build-file}/item.json";
}
}