-1

Good morning, I want to improve a GUI with QT c++ to read, edit and save info in a txt file. This is what I have:

  1. A txt file with some parameters written (like "A=1 B=5 ...")
  2. And I have been able to do the following with my qt code in c++ :

    2.1. I have created a GUI where those parameters (A, B, ...) will be shown in labels, and its values will be shown in lineEdits (1, 5, ...)

    2.2. I can edit the lineEdits, but I am not sure how to save them

My question is:

How can I save the info? Should I make the code to save the changes everytime anything change? Or all together at the end? or...? I do not know enought to make this.

CODES

To show the values, I used this:

const auto &config = AgCommConfig::getInstance(); //here my config.cpp was getting the info of the txt file

ui->lineEdit->setText(QString("%1").arg(config.speedchange())); //here my lineEdit shows the info of this txt file

And to save the info back in the txt file, I was starting with this:

void MainWindow::on_lineEdit_editingFinished()
{
    QString input = ui->lineEdit->text();
}

I have the variable now, but I do not know if the fuction is the correct one (I think yes), and I do not know how to continue to overwrite the info in the txt file

. . EDIT:

link to my picture

I suppose that the easiest way is to save (overwrite) all the data in 1 go as you said, and not continously.

I do not want to change the labels. Only the values, and update them in the txt file.

So mainly, I suppose that for each value, I should have 1 variable. And it will be udpated everytime with the fuction ''editingfinished''. When he click in a new ''save all'' button, the code should create a new txt file with the same name to overwrite the old one.

But the problem are the comments... I could maybe save them in a variable, but they will be hard to order them again.

Tell me please if there is any other easiest way in any point, or in total

Thanks in advance a lot!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Minikornio
  • 47
  • 5

1 Answers1

0

When to save the information to file is a requirement driven question. You can save all the data in one go or continuously. Since (I'm guessing here) it seems like the value update isn't limited to a sequence, I'd suggest updating / creating the file at the end of the work flow.

You have used a correct function for obtaining the current text from the QLineEdit.

Now for updating the values from QLineEdit to QLabel. You can either update the label value directly inside the line edit slot. Something like this:

void MainWindow::on_lineEdit_editingFinished()
{
    QString input = ui->lineEdit->text();
    ui->label->setText(input);
}

Or you can connect the QLineEdit::textChanged(const QString &text) signal to the QLabel::setText(const QString &) slot. The limitation here would be that you seem to only wish an update after editing is completed, this would keep updating it. Secondly, the previous method could allow you to validate your input before setting the value to the label. You can do this by typing in the connect statement or connecting them while the designer (I've never used it personally).

Another alternative(my recommendation) would be to have a QPushButton [Look into QAbstractButton::clicked(bool checked = false)] (Name it "Set" or something) for setting the value. In this case once the button is pressed, you can copy the text from the LineEdit to the Label. It might also be a good time to save the values to your file.

Writing to file can be accomplished using the QFile class. You might wish to truncate the file when opening it and write all the variables to make it easier.

A lot of my answer is me guessing what you are doing. A screenshot of your GUI might be helpful here.

Edit: First thing I want to ask is if you really need a variable to store the values. I mean you are just gonna end up converting them to a string for writing to a file. The ui object's variable should do (just use ui->lineEdit->text() when its needed).

As for writing while preserving the comments, you can use QTextStream. You can read and update the file line by line. Parse the line, update it, and finally write it back to the file.

Bhoot
  • 181
  • 1
  • 10
  • `QString input = ui->lineEdit->text(); ui->lineEdit->setText(input);`???? Are you going to put the text of the same QLineEdit to the same QLineEdit? – eyllanesc Oct 22 '18 at 10:18
  • Sorry about that. I just copy pasted it from the question. Should be label in the second line. – Bhoot Oct 22 '18 at 10:37