Qt 5.8。谁能解释一下如何将 json 从 QLineEdit 保存到 text.json 文件?

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

我想通过按钮简单地将新数据从 QLineEdit 保存到 text.json。如果我按下按钮,那么我想在 5 行编辑中输入所有数据。然后我想通过单击按钮保存所有数据。我先谢谢你了。

那是我的 .Cpp 文件

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()
{ }

那是我的.H文件

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

如何将按钮与 LineEdits 连接?

您有 2 个选择,要么将您的 QLineEdits 信号
void QLineEdit::editingFinished() 连接到自动将信息保存到局部变量的插槽,要么通过访问QLineEdits 文本,方法是使用 ui->yourLineEdit1->text() 并将其存储到每个局部变量。

类似于:

// 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);     
}

虽然将字段作为 class 成员似乎完全多余,但您可以完全跳过该步骤并直接从 JSON 读取到 GUI,然后从 GUI 写入 JSON .