Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

281
Vistas
¿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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda