-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileio.cpp
43 lines (37 loc) · 1.31 KB
/
fileio.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
#include "fileio.h"
FileIO::FileIO(QObject *parent) : QObject(parent) {}
bool FileIO::writeFile(const QString &filePath, const QString &data) {
QFile file(filePath);
if (file.open(QFile::WriteOnly)) {
//这里使用QJsonDocument转换一次,规避windows和Linux平台 不一致
//学习其他项目的经验
auto json = QJsonDocument::fromJson(data.toUtf8());
auto data = json.toJson();
file.write(data);
file.close();
mError.clear();
return true;
} else {
mError = "FileName: " + file.fileName() + " Error: " + file.errorString();
return false;
}
}
QString FileIO::readFile(const QString &filePath) {
QString ret;
QFile file(filePath);
if (file.open(QFile::ReadOnly)) {
auto bytes = file.readAll();
//如果Json中的字符串被手动编辑时,按下了回车键,qml中的JSON.parse会认不出来。所以这里用QJsonDocument格式化一下,再给出去。
auto json = QJsonDocument::fromJson(bytes);
ret = QString(json.toJson());
file.close();
mError.clear();
} else {
ret.clear();
mError = "FileName: " + file.fileName() + " Error: " + file.errorString();
}
return ret;
}
const QString &FileIO::errorString() {
return mError;
}