Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

280
Views
¿Cómo puedo guardar JSON en un archivo de texto local en QT?
jsonlist.push(Object.assign(process1_json,process2_json,process3_json,process4_json,process5_json, process6_json)) var j = JSON.stringify(jsonlist);

Estructura del proyecto

Puedo usar console.log para obtener una salida JSON, pero quiero generar un archivo JSON local.

Parece que JavaScript no puede leer/escribir archivos locales directamente, así que si tengo que convertir JSON a QJson, ¿obtengo un archivo JSON local?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede hacerlo convirtiendo su JSON en un QJsonObject en C++ y luego guárdelo con esa función:

 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; }
about 4 years ago · Juan Pablo Isaza Report

0

Gracias de todos modos. Lo he resuelto introduciendo el módulo 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(); }

Después de registrarse en main.cpp ,

 qmlRegisterType<FileContent>("test.filecontent", 1, 0, "FileContentItem");

solo importando este módulo,

 import test.filecontent 1.0

y usando como sigue:

 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 } }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!