Search code examples
jsonqtqlineedit

Qt 5.8. Can anyone explain me how can i save json from QLineEdit to a text.json file?


I want simple save new data from QLineEdit in to text.json by push button. If i push the button, then i want to enter all data in 5 LineEdits. Then i want to save all data by button click. I thank you in advance.

That is my .Cpp file

include "address.h"
include "ui_address.h"

Address::Address(QWidget *parent) : QDialog(parent), ui(new Ui::Address)
{
 ui->setupUi(this);
 connect(ui->pb_Cancel,SIGNAL(clicked(bool)),this,SLOT(close()));
}
Address::~Address()
{
 delete ui;
}

void Address::read(const QJsonObject &json)
{
 mVorname= json["vorname"].toString();
 mNachname= json["nachname"].toString();
 mLand= json["land"].toString();
 mName= json["name"].toString();
 mPassword= json["password"].toString();
}

void Address::write(QJsonObject &json) const
{
 json["vorname"]= mVorname;
 json["nachmane"]= mNachname;
 json["land"]= mLand;
 json["name"]= mName;
 json["password"]= mPassword;
}
bool Address::saveData(Address::SaveFormat saveFormat) const
{
 QFile saveFile(saveFormat == Json ? 
 QStringLiteral("data.json"):QStringLiteral("save.dat"));

 QJsonObject dataObject;
 write(dataObject);
 QJsonDocument saveDoc(dataObject);
 saveFile.write(saveFormat == Json ? 
 saveDoc.toJson():saveDoc.toBinaryData());
 return true;
}

void Address::on_pb_save_clicked()
{ }

That is my .H file

ifndef ADDRESS_H
define ADDRESS_H

include <QDialog>
include <QFile>
include <QJsonObject>
include <QJsonArray>
include <QJsonDocument>
include <QString>
include <QDir>
include "signalslotsample.h"
include <QTemporaryFile>

namespace Ui
{
 class Address;
}
class Address : public QDialog
{
 Q_OBJECT
 public:
 explicit Address(QWidget *parent = 0);
~Address();
 enum SaveFormat
{
  Json
};
Address(const QString &vorname, QString &nachname, QString &land, 
QString &name, QString &password);

 bool saveData(SaveFormat saveFormat)const;

 QString vorname()const;
 void setVorname(const QString &vorname);

 QString nachname()const;
 void setNachname(const QString &nachname);

 QString land()const;
 void setLand(const QString &land);

 QString name()const;
 void setName(const QString &name);

 QString password()const;
 void setPassword(const QString &password);

 void read(const QJsonObject &json);
 void write(QJsonObject &json)const;

private slots:
 void on_pb_save_clicked();

private:
 Ui::Address *ui;
 QString mVorname;
 QString mNachname;
 QString mLand;
 QString mName;
 QString mPassword;
 QVariant mQtData;
};
endif // ADDRESS_H

enter image description here

How can i connect a button with LineEdits?


Solution

  • Something like:

    // in the constructor
    connect(ui->pb_Save,SIGNAL(clicked(bool)),this,SLOT(on_pb_save_clicked()));
    
    
    void Address::on_pb_save_clicked() {
         mVorname= ui->theVornameLabel->text();
         mNachname= ui->theNachnameLabel->text();
         // ...
         // and so on for the rest and then 
         saveData(saveFormat);     
    }
    

    Although having the fields as class members seems entirely redundant, you can completely skip that step and directly read from JSON to the GUI, and write from the GUI to JSON.