[solved] How to set the changed text as default text? (for lineEdit)
Hi, I use ‘lineEdit’ in my program. And as you know, we can set the default text before running the program. And then when the program starts, we can change the text of the lineEdit. But how to set the changed text as default text? So that when the program exits and starts again, the text of the lineEdit is the new one.
Does anyone know how to do? Thanks!
14 replies
Here’s the code how to use QSettings:
Create two functions saveSettings() and readSettings as follows:
- void Widget::readSettings()
- {
- yourVariableContainingText = settings.value("mytext").toString();
- ui->lineEdit->setText(yourVariableContainingText);
- }
- void Widget:saveSettings
- {
- settings.setValue("mytext", ui->lineEdit->text());
- }
Then call saveSettings() on your destructor and readSettings() on your constructor.
Don’t forget to create a private variable for storing text:
Hi, Milot Shala & Immii, thank you for your information!
I used the say method, an ‘ini’ file of QSettings to solve my question and got what I want. Below is my code.
- void MainWindow::on_setButton_clicked()
- {
- settings.setValue("offset0", str_1);
- settings.setValue("value0", str0);
- ...
- }
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- if (fi.exists())
- {
- ui->lineEdit_offset0->setText(str_1);
- ui->lineEdit_value0->setText(str0);
- ...
- }
- }
justforfun, just as a simplification: QSettings::value() [doc.trolltech.com] takes an optional second parameter for a default value. This way you can always read from your settings, regardless if it exists or not.
Also, in line 20 you check for file “myconfig.ini” in line 23 you open “ulsconfig.ini”. Seems to be a typo when preparing the code for the forum here, but better have a check.
justforfun, just as a simplification: QSettings::value() [doc.trolltech.com] takes an optional second parameter for a default value. This way you can always read from your settings, regardless if it exists or not.Also, in line 20 you check for file “myconfig.ini” in line 23 you open “***config.ini”. Seems to be a typo when preparing the code for the forum here, but better have a check.
Hi Volker, thank you for your hint! That is a good way, I learn it from you now.
As fro the typo, you are right. I corrected it.
You must log in to post a reply. Not a member yet? Register here!



