mercredi 29 juillet 2020

How to pass a JSON object inside a QTextEdit

I have a small GUI that I use to load/save json configuration files, the most important parameters are in the gui below:

conf

The problem I have been trying to solve is that I am not able to create an object inside a QTextEdit and am not sure why despite I am following official documentation on how to do that.

Below a snippet of code both for the load and save button. Also for the sake of brevity I only kept how I did the spinbox and, of course, the textedit:

void SettingsForm::on_loadBtn_clicked()
{
  // Opening file dialog....

  if(listDocksConfig.isEmpty())
  {
    QMessageBox::information(this, tr("Message"), tr("Please Open Configuration"));
  }
  else
  {
    QJsonDocument doc;
    QJsonObject obj;
    QByteArray data_json;
    QFile input(listDocksConfig);
    if(input.open(QIODevice::ReadOnly | QIODevice::Text))
    {
      data_json = input.readAll();
      doc = doc.fromJson(data_json);
      obj = doc.object();

      const double xposValue = obj["X Pos"].toDouble();
      QTextEdit textEdit     = QTextEdit::setText(obj["comments"]);  // <- Error Here

      ui->doubleSpinBox_XPos->setValue(xposValue);
      ui->textEdit->setText(textEdit);      // <- Error Here
    }
    else
    {
       // do something
    }
  }
}

void SettingsForm::on_saveBtn_clicked()
{

  // saving configuration with file dialog....

  if(listDocksConfig.isEmpty())
  {
    // do something...
  }
  else
  {
    const double xposValue = ui->doubleSpinBox_XPos->value();
    QTextEdit textEdit     = ui->textEdit->setPlainText();  // <- Error Here

    QJsonDocument doc;
    QJsonObject obj;

    obj["X Pos"] = xposValue;
    obj["comments"] = textEdit.toString();     // <- Error Here

    doc.setObject(obj);
    QByteArray data_json = doc.toJson();
    QFile output(listDocksConfig);
  }
}

What I have done so far:

  1. I consulted the official documentation on how to solve this problem, but could not figure out why that was not working. I also went ahead and try to use an alternative such as setText but still no luck.

  2. I came across this source which I used as guidance for my example and solved almost all problems but the QTextEdit one.

  3. This additional post was useful but still couldn't solve the problem.

Thanks for pointing to the right direction for solving this problem.

Aucun commentaire:

Enregistrer un commentaire