jsonlist.push(Object.assign(process1_json,process2_json,process3_json,process4_json,process5_json, process6_json))
var j = JSON.stringify(jsonlist);

我可以console.log用來獲取 JSON 輸出,但我想生成一個本地 JSON 檔案。
好像 JavaScript 不能直接讀/寫本地檔案,所以如果我必須將 JSON 轉換為 QJson 然后獲取本地 JSON 檔案?
uj5u.com熱心網友回復:
您可以通過將 JSON 轉換為QJsonObjectC 中的a來實作,然后使用該函式保存它:
bool writeJson(const QString &fileName, const QJsonObject &object)
{
//open file
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
qDebug() << QString("Fail to open the file:" fileName);
return false;
}
//convert to document
QJsonDocument d = QJsonDocument(object);
//write and close
file.write(d.toJson());
file.close();
return true;
}
uj5u.com熱心網友回復:
還是很感謝你。我已經通過引入 C 模塊解決了它。
#ifndef FILECONTENT_H
#define FILECONTENT_H
#include <QObject>
#include <QFile>
#include <QTextStream>
class FileContent : public QObject
{
Q_OBJECT
public:
Q_PROPERTY(QString content READ getContent WRITE setContent)
Q_PROPERTY(QString filename READ getFileName WRITE setFileName)
Q_PROPERTY(QString dir READ getDir WRITE setDir)
Q_INVOKABLE QString getContent();
Q_INVOKABLE void setContent(QString s);
Q_INVOKABLE QString getFileName();
Q_INVOKABLE QString getDir();
Q_INVOKABLE void setDir(QString s);
FileContent(QObject *parent = 0);
~FileContent();
private:
QFile *file;
QString content;
QString filename;
QString dir;
public slots:
void setFileName(const QString& filename);
void clearContent();
};
#endif // FILECONTENT_H
#include "filecontent.h"
#include <QDebug>
FileContent::FileContent(QObject *parent) {
}
FileContent::~FileContent() {
delete file;
}
QString FileContent::getFileName() {
return this->filename;
}
QString FileContent::getDir()
{
//I need to get file from specific directory path
QString envVarName_equip = "MPCVD_MODEL";
this->dir=qEnvironmentVariable(envVarName_equip.toStdString().c_str());
return dir '\\';
}
void FileContent::setDir(QString s)
{
file = new QFile(s);
}
void FileContent::setFileName(const QString &filename) {
this->filename = filename;
file = new QFile(getDir() '\\' filename);
}
QString FileContent::getContent() {
if( content.length() == 0 ) {
file->open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(file);
in.setCodec("UTF-8");//set format here
content = in.readAll();
if( content.length() == 0) {
qDebug() << "[Warning] FileContent: file " << this->filename << "is empty" << endl;
}
}
return content;
}
void FileContent::setContent(QString s)
{
file->open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream in(file);
in.setCodec("UTF-8");//set format here
in<<s;
}
void FileContent::clearContent() {
content.clear();
}
注冊后main.cpp,
qmlRegisterType<FileContent>("test.filecontent", 1, 0, "FileContentItem");
只需匯入這個模塊,
import test.filecontent 1.0
并使用如下:
Button{
id:genjson
text: qsTr("generate JSON")
onClicked: {
fileDialog.processContent(new Date().toLocaleString(Qt.locale(), 'yyyyMMdd') ".mpcvd",CJS.genjson())
}
}
FileContentItem {
id: fileDialog
filename: "default.mpcvd"
property bool ready: false
function processContent(source,content) {
if( source !== undefined ) {
filename = source;
}
setContent(content);
clearContent(); // save memory
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/333842.html
標籤:javascript qt 质量管理器 文件
