[solved] How to set the changed text as default text? (for lineEdit)
-
Here's the code how to use QSettings:
Create two functions saveSettings() and readSettings as follows:
@
void Widget::readSettings()
{
QSettings settings("company_name", "product");
yourVariableContainingText = settings.value("mytext").toString();
ui->lineEdit->setText(yourVariableContainingText);
}void Widget:saveSettings
{
QSettings settings("company_name", "product");
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:
@
class Widget : public QWidget
{
public:
...private:
QString yourVariableContainingText;
};
@ -
Your constructor or init() method shoule look something like this:
@
myClass::myClass()
{
QString defText=readSettings();
If(defText==NULL)
myLientEdit.setText("Default Text")
else
myLineEdit.setText(defText);
}myClass::~myClass()
{
saveSettings();
}
@
Hope this clarifies further.[Edit: set indents into code / Vass]
-
[quote author="peppe" date="1293300321"]What's the default text you're talking about? the "placeholderText"?[/quote]
No. I mean 'text' filed of a lineEdit.
-
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()
{
QSettings settings("myconfig.ini", QSettings::IniFormat);QString str_1 = ui->lineEdit_offset0->text(); settings.setValue("offset0", str_1); QString str0 = ui->lineEdit_value0->text(); settings.setValue("value0", str0); ...
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);QFileInfo fi("myconfig.ini"); if (fi.exists()) { QSettings settings("myconfig.ini", QSettings::IniFormat); QString str_1 = settings.value("offset0").toString(); ui->lineEdit_offset0->setText(str_1); QString str0 = settings.value("value0").toString(); ui->lineEdit_value0->setText(str0); ... }
}@
-
[quote author="justforfun" date="1293371341"]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.
[/quote]I am glad so will be Milot Shala that your problem is solved. Please mark this thread solved.:)
-
justforfun, just as a simplification: "QSettings::value() ":http://doc.trolltech.com/stable/qsettings.html#value 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.
-
[quote author="Volker" date="1293385022"]justforfun, just as a simplification: "QSettings::value() ":http://doc.trolltech.com/stable/qsettings.html#value 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.[/quote]
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. -
[quote author="Immii" date="1293382684"]
[quote author="justforfun" date="1293371341"]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.
[/quote]I am glad so will be Milot Shala that your problem is solved. Please mark this thread solved.:)[/quote]
Added.
-
Good to hear that the problems was solved. Please don't forget to mark this thread as [SOLVED]
-
[quote author="Milot Shala" date="1293441272"]Good to hear that the problems was solved. Please don't forget to mark this thread as [SOLVED][/quote]
Hi, I pressed the 'Tag -> solved' already, is it right?
-
[quote author="justforfun" date="1293442179"]
[quote author="Milot Shala" date="1293441272"]Good to hear that the problems was solved. Please don't forget to mark this thread as [SOLVED][/quote]Hi, I pressed the 'Tag -> solved' already, is it right?
[/quote]
You can change the title as well, just prepend the [SOLVED] before the actual title.
-
[quote author="Milot Shala" date="1293442420"]
[quote author="justforfun" date="1293442179"]
[quote author="Milot Shala" date="1293441272"]Good to hear that the problems was solved. Please don't forget to mark this thread as [SOLVED][/quote]Hi, I pressed the 'Tag -> solved' already, is it right?
[/quote]
You can change the title as well, just prepend the [SOLVED] before the actual title.[/quote]
Done.