Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
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.11 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 11.8k 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.
  • W Offline
    W Offline
    wtimm
    wrote on last edited by
    #11

    if i use originalLength function i got use of undeclared identifier 'originalLength' error

    JonBJ 1 Reply Last reply
    0
    • J.HilkJ J.Hilk

      @wtimm

      auto val = ui->lineEdit->text().toInt();
      auto originalLength = ui->lineEdit->text().length();
      val += ui->spinBox->value();
      ui->lineEdit->setText(QString::number(val).rightJustified(originalLength, '0'));
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #12

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

      auto originalLength = ui->lineEdit->text().length();

      @wtimm Do you have this in your code?


      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
      0
      • W wtimm

        if i use originalLength function i got use of undeclared identifier 'originalLength' error

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #13

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

        if i use originalLength function i got use of undeclared identifier 'originalLength' error

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

        auto originalLength = ui->lineEdit->text().length();
        ui->lineEdit->setText(QString::number(val).rightJustified(originalLength, '0'));

        It's not a "function". So how did you get "undeclared identifier"?

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

          you are right it is not a function , thanks all of you for your help

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

            7ee362aa-8649-4386-99cc-d657b07aed52-image.png

            a8040c62-0a1f-4f42-a388-768b01364570-image.png

            how can i see '0547' as default value when i run the project again

            JonBJ 1 Reply Last reply
            0
            • W wtimm

              7ee362aa-8649-4386-99cc-d657b07aed52-image.png

              a8040c62-0a1f-4f42-a388-768b01364570-image.png

              how can i see '0547' as default value when i run the project again

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #16

              @wtimm
              Well you need to save that value to some permanent storage to be able to retrieve it next time! Unless you are using a database in your application Qt's QSettings is the most convenient way.

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

                int v = settings.value(number(val),defaultValue).toInt();

                I wrote such a code but it didn't work.
                When I save the increased value, how can I make the increased value show as default when I open the project again?

                Pl45m4P JonBJ 2 Replies Last reply
                0
                • W wtimm

                  int v = settings.value(number(val),defaultValue).toInt();

                  I wrote such a code but it didn't work.
                  When I save the increased value, how can I make the increased value show as default when I open the project again?

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

                  @wtimm 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?

                  Save the increased (always the current) value when you close your project?!


                  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 wtimm

                    int v = settings.value(number(val),defaultValue).toInt();

                    I wrote such a code but it didn't work.
                    When I save the increased value, how can I make the increased value show as default when I open the project again?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #19

                    @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 1 Reply Last reply
                    0
                    • Pl45m4P Pl45m4

                      @wtimm 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?

                      Save the increased (always the current) value when you close your project?!

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

                      @Pl45m4 how can i save this , may i use QSettings or different class

                      JonBJ 1 Reply Last reply
                      0
                      • W wtimm

                        @Pl45m4 how can i save this , may i use QSettings or different class

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #21

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

                        W 2 Replies Last reply
                        0
                        • 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 Offline
                                      JonBJ Offline
                                      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

                                          • Login

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