Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I want to write a code that increases the value in the line edit when the checkbox is clicked.
Qt 6.12 LTS is out! See what's new in the release blog

I want to write a code that increases the value in the line edit when the checkbox is clicked.

Scheduled Pinned Locked Moved Unsolved General and Desktop
46 Posts 8 Posters 12.2k Views 3 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.
  • JonBJ JonB

    @wtimm Already told you to save it via QSettings, there really isn't anything else to say....

    W Offline
    W Offline
    wtimm
    wrote on last edited by
    #22

    @JonB we have defined the variable val in the above answers (the value in the line edit)

    Can we do it if we define val inside the setValue function or should we do it using another function?

    void Widget::on_grabDataButton_clicked()
    {
    QSettings settings;
    settings.setValue("val",1);
    settings.value("val").toInt();
    }

    Pl45m4P 1 Reply Last reply
    0
    • W wtimm

      @JonB we have defined the variable val in the above answers (the value in the line edit)

      Can we do it if we define val inside the setValue function or should we do it using another function?

      void Widget::on_grabDataButton_clicked()
      {
      QSettings settings;
      settings.setValue("val",1);
      settings.value("val").toInt();
      }

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #23

      @wtimm said in I want to write a code that increases the value in the line edit when the checkbox is clicked.:

      void Widget::on_grabDataButton_clicked()
      {
      QSettings settings;
      settings.setValue("val",1);
      settings.value("val").toInt();
      }

      Dont know what this should do. Maybe you are using QSettings wrong.

      To set value
      settings.setValue("val", 42);

      To read the value:
      int val = settings.value("val").toInt();

      And do whatever you like with val (set it to your line edit after you start your program)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      3
      • JonBJ JonB

        @wtimm Already told you to save it via QSettings, there really isn't anything else to say....

        W Offline
        W Offline
        wtimm
        wrote on last edited by
        #24

        @JonB can you take a look at the code i wrote, where is my mistake ?

        Pl45m4P 1 Reply Last reply
        0
        • W wtimm

          @JonB can you take a look at the code i wrote, where is my mistake ?

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #25

          @wtimm

          The mistake is that it seems that you are not doing something with your value. It makes no sense to write and read the setting at the same place of your code (every time you click a button). Read it, where you need the value.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          1
          • W Offline
            W Offline
            wtimm
            wrote on last edited by
            #26

            @Pl45m4

            void Widget::on_grabDataButton_clicked()
            {
            }

            void Widget::on_mt_IncreaseValueButton_clicked()
            {
            auto originalLength = ui->mt_SerialNumberLineEdit->text().length();
            auto val = ui->mt_SerialNumberLineEdit->text().toInt();
            val += ui->mt_SpinBox->value();
            ui->mt_SerialNumberLineEdit->setText(QString::number(val));
            ui->mt_SerialNumberLineEdit->setText(QString::number(val).rightJustified(originalLength , '0'));
            }
            QSettings settings;
            settings.setValue("val",42);
            settings.value("val").toInt();

            where should i use code in

            Pl45m4P SGaistS 2 Replies Last reply
            0
            • W wtimm

              @Pl45m4

              void Widget::on_grabDataButton_clicked()
              {
              }

              void Widget::on_mt_IncreaseValueButton_clicked()
              {
              auto originalLength = ui->mt_SerialNumberLineEdit->text().length();
              auto val = ui->mt_SerialNumberLineEdit->text().toInt();
              val += ui->mt_SpinBox->value();
              ui->mt_SerialNumberLineEdit->setText(QString::number(val));
              ui->mt_SerialNumberLineEdit->setText(QString::number(val).rightJustified(originalLength , '0'));
              }
              QSettings settings;
              settings.setValue("val",42);
              settings.value("val").toInt();

              where should i use code in

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #27

              @wtimm said in I want to write a code that increases the value in the line edit when the checkbox is clicked.:

              where should i use code in

              Not in your buttonClicked handlers.

              You save the current value one time, when you close your program and read it one time to write it back to your lineEdit, when you start your program.

              e.g. your D'tor to save your value (obviously before you destroy your GUI) and maybe showEvent() to read your settings.


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              1
              • JonBJ JonB

                @wtimm said in I want to write a code that increases the value in the line edit when the checkbox is clicked.:

                I wrote such a code but it didn't work.

                Meaning what? Please don't just say "it didn't work" if you want people to understand enough to help. Your code shows reading back a value. How do we know what you saved where how? Did you look in the external INI file or Windows Registry to see what is there which has been saved?

                When I save the increased value, how can I make the increased value show as default when I open the project again?

                Read it back in somewhere at startup and set the widget's value from the previously saved.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #28

                @JonB said in I want to write a code that increases the value in the line edit when the checkbox is clicked.:

                When I save the increased value, how can I make the increased value show as default when I open the project again?

                Read it back in somewhere at startup and set the widget's value from the previously saved.

                1 Reply Last reply
                0
                • W wtimm

                  @Pl45m4

                  void Widget::on_grabDataButton_clicked()
                  {
                  }

                  void Widget::on_mt_IncreaseValueButton_clicked()
                  {
                  auto originalLength = ui->mt_SerialNumberLineEdit->text().length();
                  auto val = ui->mt_SerialNumberLineEdit->text().toInt();
                  val += ui->mt_SpinBox->value();
                  ui->mt_SerialNumberLineEdit->setText(QString::number(val));
                  ui->mt_SerialNumberLineEdit->setText(QString::number(val).rightJustified(originalLength , '0'));
                  }
                  QSettings settings;
                  settings.setValue("val",42);
                  settings.value("val").toInt();

                  where should i use code in

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #29

                  Hi,

                  @wtimm said in I want to write a code that increases the value in the line edit when the checkbox is clicked.:

                  settings.value("val").toInt();

                  What exactly do you think that line is doing ?
                  How can it change anything with regards to your widgets ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • W Offline
                    W Offline
                    wtimm
                    wrote on last edited by
                    #30

                    i don't quite understand how can i do this

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #31

                      As others have already written:

                      • read the settings into a variable
                      • do any computation you might need (it might be none)
                      • set the result on the widget that is supposed to contain it

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2
                      • W Offline
                        W Offline
                        wtimm
                        wrote on last edited by
                        #32

                        I can't code it

                        JonBJ 1 Reply Last reply
                        0
                        • W wtimm

                          I can't code it

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by
                          #33

                          @wtimm
                          What about Googling for, say, QSettings examples? For example, the wiki's How to Use QSettings looks like it has an example like yours?

                          1 Reply Last reply
                          1
                          • W Offline
                            W Offline
                            wtimm
                            wrote on last edited by
                            #34

                            i searched and tried but i can't code it

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by SGaist
                              #35

                              You do realize that between the wiki page @JonB linked to that is doing what you and the "restoring the state of a GUI application" example of the QSettings class, you have everything you need to write that part for your application ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1
                              • W Offline
                                W Offline
                                wtimm
                                wrote on last edited by
                                #36

                                void Widget::on_mt_IncreaseValueButton_clicked()
                                {
                                auto originalLength = ui->mt_SerialNumberLineEdit->text().length();
                                auto val = ui->mt_SerialNumberLineEdit->text().toInt();
                                val += ui->mt_SpinBox->value();
                                ui->mt_SerialNumberLineEdit->setText(QString::number(val));
                                ui->mt_SerialNumberLineEdit->setText(QString::number(val).rightJustified(originalLength , '0'));
                                }

                                How can I print and read the val variable here in the file ? I also want it to overwrite the value continuously.

                                1 Reply Last reply
                                0
                                • W Offline
                                  W Offline
                                  wtimm
                                  wrote on last edited by
                                  #37

                                  bool WriteFile(){

                                  QFile file("mt_SerialNumber.txt");
                                  
                                  QTextStream stream(&file);
                                  while(val != NULL)
                                  {
                                      stream << QString::number(val);
                                  }
                                  
                                  return true;
                                  

                                  }

                                  I defined something like this to main.cpp, but it says undeclared identifier because I can't pull val

                                  Pl45m4P 1 Reply Last reply
                                  0
                                  • W wtimm

                                    bool WriteFile(){

                                    QFile file("mt_SerialNumber.txt");
                                    
                                    QTextStream stream(&file);
                                    while(val != NULL)
                                    {
                                        stream << QString::number(val);
                                    }
                                    
                                    return true;
                                    

                                    }

                                    I defined something like this to main.cpp, but it says undeclared identifier because I can't pull val

                                    Pl45m4P Offline
                                    Pl45m4P Offline
                                    Pl45m4
                                    wrote on last edited by
                                    #38

                                    @wtimm

                                    Do you even read what we write?

                                    I defined something like this to main.cpp, but it says undeclared identifier because I can't pull val

                                    Because you probably dont have val in your main.cpp. Return it there from your object instance of your widget.


                                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                    ~E. W. Dijkstra

                                    W 1 Reply Last reply
                                    0
                                    • W Offline
                                      W Offline
                                      wtimm
                                      wrote on last edited by
                                      #39

                                      i tried to return but i got a error message like void function should not return a value

                                      mrjjM JonBJ 2 Replies Last reply
                                      0
                                      • W wtimm

                                        i tried to return but i got a error message like void function should not return a value

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #40

                                        @wtimm
                                        that means you have something like

                                        void func() {
                                        return 20;
                                        }

                                        so you need to change the return type unless its a slot as then it wont work as you think.

                                        1 Reply Last reply
                                        0
                                        • W wtimm

                                          i tried to return but i got a error message like void function should not return a value

                                          JonBJ Online
                                          JonBJ Online
                                          JonB
                                          wrote on last edited by
                                          #41

                                          @wtimm
                                          I do not mean to be unkind, but do you not think you should read some books or tutorials about C++ programming? From your problems you need to understand some of the basics, your questions are not really about Qt.

                                          1 Reply Last reply
                                          2

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt
                                          • Unsolved