December 25, 2010

justforfun justforfun
Lab Rat
27 posts

[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

December 25, 2010

peppe peppe
Ant Farmer
1026 posts

What’s the default text you’re talking about? the “placeholderText”?

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

December 25, 2010

Immii Immii
Ant Farmer
233 posts

what you can do here is save the changed text using QSettings and on reload of the application read this and set as default.
QSettings is persistent platform-independent application settings.

December 25, 2010

Milot Shala Milot Shala
Lab Rat
393 posts

Here’s the code how to use QSettings:

Create two functions saveSettings() and readSettings as follows:

  1. void Widget::readSettings()
  2. {
  3.    QSettings settings("company_name", "product");
  4.    yourVariableContainingText = settings.value("mytext").toString();
  5.    ui->lineEdit->setText(yourVariableContainingText);
  6. }
  7.  
  8. void Widget:saveSettings
  9. {
  10.    QSettings settings("company_name", "product");
  11.    settings.setValue("mytext", ui->lineEdit->text());
  12. }

Then call saveSettings() on your destructor and readSettings() on your constructor.

Don’t forget to create a private variable for storing text:

  1. class Widget : public QWidget
  2. {
  3.   public:
  4.    ...
  5.  
  6.   private:
  7.    QString yourVariableContainingText;
  8. };

December 25, 2010

Immii Immii
Ant Farmer
233 posts

Your constructor or init() method shoule look something like this:

  1. myClass::myClass()
  2. {
  3.    QString defText=readSettings();
  4.    If(defText==NULL)
  5.         myLientEdit.setText("Default Text")
  6.    else
  7.         myLineEdit.setText(defText);
  8. }
  9.  
  10. myClass::~myClass()
  11. {
  12.     saveSettings();
  13. }

Hope this clarifies further.

Edit: set indents into code / Vass

December 26, 2010

justforfun justforfun
Lab Rat
27 posts
peppe wrote:
What’s the default text you’re talking about? the “placeholderText”?

No. I mean ‘text’ filed of a lineEdit.

December 26, 2010

justforfun justforfun
Lab Rat
27 posts

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.

  1. void MainWindow::on_setButton_clicked()
  2. {
  3.     QSettings settings("myconfig.ini", QSettings::IniFormat);
  4.  
  5.     QString str_1 = ui->lineEdit_offset0->text();
  6.     settings.setValue("offset0", str_1);
  7.     QString str0 = ui->lineEdit_value0->text();
  8.     settings.setValue("value0", str0);
  9.     ...
  10. }
  11.  
  12.  
  13.  
  14. MainWindow::MainWindow(QWidget *parent) :
  15.     QMainWindow(parent),
  16.     ui(new Ui::MainWindow)
  17. {
  18.     ui->setupUi(this);
  19.  
  20.     QFileInfo fi("myconfig.ini");
  21.     if (fi.exists())
  22.     {
  23.         QSettings settings("myconfig.ini", QSettings::IniFormat);
  24.  
  25.         QString str_1 = settings.value("offset0").toString();
  26.         ui->lineEdit_offset0->setText(str_1);
  27.         QString str0 = settings.value("value0").toString();
  28.         ui->lineEdit_value0->setText(str0);
  29.         ...
  30.      }
  31. }

December 26, 2010

Immii Immii
Ant Farmer
233 posts
justforfun wrote:
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.

I am glad so will be Milot Shala that your problem is solved. Please mark this thread solved.:)

December 26, 2010

Volker Volker
Robot Herder
5428 posts

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.

December 27, 2010

justforfun justforfun
Lab Rat
27 posts

Volker wrote:
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.

December 27, 2010

justforfun justforfun
Lab Rat
27 posts
Immii wrote:
justforfun wrote:
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.

I am glad so will be Milot Shala that your problem is solved. Please mark this thread solved.:)

Added.

December 27, 2010

Milot Shala Milot Shala
Lab Rat
393 posts

Good to hear that the problems was solved. Please don’t forget to mark this thread as [SOLVED]

December 27, 2010

justforfun justforfun
Lab Rat
27 posts
Milot Shala wrote:
Good to hear that the problems was solved. Please don’t forget to mark this thread as [SOLVED]

Hi, I pressed the ‘Tag -> solved’ already, is it right?

December 27, 2010

Milot Shala Milot Shala
Lab Rat
393 posts
justforfun wrote:
Milot Shala wrote:
Good to hear that the problems was solved. Please don’t forget to mark this thread as [SOLVED]

Hi, I pressed the ‘Tag -> solved’ already, is it right?

You can change the title as well, just prepend the [SOLVED] before the actual title.

December 27, 2010

justforfun justforfun
Lab Rat
27 posts
Milot Shala wrote:
justforfun wrote:
Milot Shala wrote:
Good to hear that the problems was solved. Please don’t forget to mark this thread as [SOLVED]

Hi, I pressed the ‘Tag -> solved’ already, is it right?

You can change the title as well, just prepend the [SOLVED] before the actual title.

Done.

 
  ‹‹ QTextEdit highlight all characters in a given range      Behavior of paint method in QGraphicsItem ››

You must log in to post a reply. Not a member yet? Register here!