Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] How to set the changed text as default text? (for lineEdit)
Forum Updated to NodeBB v4.3 + New Features

[solved] How to set the changed text as default text? (for lineEdit)

Scheduled Pinned Locked Moved General and Desktop
15 Posts 5 Posters 11.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    justforfun
    wrote on 25 Dec 2010, 15:17 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on 25 Dec 2010, 18:05 last edited by
      #2

      What's the default text you're talking about? the "placeholderText"?

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

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Immii
        wrote on 25 Dec 2010, 18:07 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          milot.shala
          wrote on 25 Dec 2010, 19:03 last edited by
          #4

          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;
          };
          @

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Immii
            wrote on 25 Dec 2010, 19:23 last edited by
            #5

            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]

            1 Reply Last reply
            0
            • J Offline
              J Offline
              justforfun
              wrote on 26 Dec 2010, 13:41 last edited by
              #6

              [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.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                justforfun
                wrote on 26 Dec 2010, 13:49 last edited by
                #7

                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);
                    ...
                 }
                

                }@

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  Immii
                  wrote on 26 Dec 2010, 16:58 last edited by
                  #8

                  [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.:)

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on 26 Dec 2010, 17:37 last edited by
                    #9

                    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.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      justforfun
                      wrote on 27 Dec 2010, 03:54 last edited by
                      #10

                      [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.

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        justforfun
                        wrote on 27 Dec 2010, 03:57 last edited by
                        #11

                        [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.

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          milot.shala
                          wrote on 27 Dec 2010, 09:14 last edited by
                          #12

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

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            justforfun
                            wrote on 27 Dec 2010, 09:29 last edited by
                            #13

                            [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?

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              milot.shala
                              wrote on 27 Dec 2010, 09:33 last edited by
                              #14

                              [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.

                              1 Reply Last reply
                              0
                              • J Offline
                                J Offline
                                justforfun
                                wrote on 27 Dec 2010, 09:35 last edited by
                                #15

                                [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.

                                1 Reply Last reply
                                0

                                1/15

                                25 Dec 2010, 15:17

                                • Login

                                • Login or register to search.
                                1 out of 15
                                • First post
                                  1/15
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved