Items can be saved to JSON file "items.json" (in standard location).
This commit is contained in:
31
data/filehandler.cpp
Normal file
31
data/filehandler.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include "filehandler.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QJsonDocument>
|
||||
#include <QStandardPaths>
|
||||
|
||||
bool FileHandler::saveToFile(const QJsonDocument& doc, const QString& fileName) {
|
||||
qDebug() << "saving file...";
|
||||
QString path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
|
||||
qDebug() << path;
|
||||
|
||||
QDir dir;
|
||||
if (!dir.exists(path)) {
|
||||
dir.mkpath(path);
|
||||
}
|
||||
// qDebug() << path + fileName;
|
||||
const QString filePath = path + '/' + fileName;
|
||||
QFile file(filePath);
|
||||
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
qWarning() << "can't open file";
|
||||
return false;
|
||||
}
|
||||
QTextStream out(&file);
|
||||
out.setEncoding(QStringConverter::Utf8);
|
||||
out << doc.toJson(QJsonDocument::Indented);
|
||||
return true;
|
||||
}
|
||||
|
||||
FileHandler::FileHandler() {}
|
||||
Reference in New Issue
Block a user