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. I want to write a code that increases the value in the line edit when the checkbox is clicked.
Forum Updated to NodeBB v4.3 + New Features

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 7.3k 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
    #3

    gönderilecek 1.png

    gönderilecek 2.png

    gönderilecek 3.png

    I want it to increase from U12C5 to U12C6 but when I write the code it returns 1. Is it possible to do what I want in qt

    JonBJ 1 Reply Last reply
    0
    • W wtimm

      gönderilecek 1.png

      gönderilecek 2.png

      gönderilecek 3.png

      I want it to increase from U12C5 to U12C6 but when I write the code it returns 1. Is it possible to do what I want in qt

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

      @wtimm
      Please paste code text not screenshots, so others can copy/paste etc.

      QString::number(+1) does not add one to anything. And you cannot directly add 1 to U12C5 anyway, since it is not a number. Either convert to a number, add to it, and convert it back to whatever your string represents, or write some code to add to U12C5 in whatever way your addition is supposed to work.

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

        eea98873-6d21-4296-bc0d-fbef09e99229-image.png

        what should i do to increase the value in such expression?

        JonBJ 1 Reply Last reply
        0
        • W wtimm

          eea98873-6d21-4296-bc0d-fbef09e99229-image.png

          what should i do to increase the value in such expression?

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

          @wtimm
          For that 1111, which is now a number, you could use something like:

          auto val = ui->lineEdit->text().toInt();
          val += ui->spinBox->value();
          ui->lineEdit->setText(QString::number(val));
          

          However, there is something hokey about trying to use a QSpinBox, with its own value and a button, to increase a number in a QLineEdit. In the example you now show, why not make Serial Number a QSpinBox itself and dispense with the separate incrementer and pushbutton?

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

            ui->spinBox->setValue(08246);

            The code throws an error when I enter such a value, so I thought it would be better to use QLineEdit.

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

              817e7c95-633c-48b5-9769-a53730b80fb1-image.png

              9e07ad58-3e36-4620-80ed-aa3922d05edd-image.png

              what can i do so that the leading zero doesn't go away

              J.HilkJ 1 Reply Last reply
              0
              • W wtimm

                817e7c95-633c-48b5-9769-a53730b80fb1-image.png

                9e07ad58-3e36-4620-80ed-aa3922d05edd-image.png

                what can i do so that the leading zero doesn't go away

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #9

                @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'));
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                Pl45m4P 1 Reply Last reply
                4
                • W wtimm

                  ui->spinBox->setValue(08246);

                  The code throws an error when I enter such a value, so I thought it would be better to use QLineEdit.

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

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

                  ui->spinBox->setValue(08246);
                  The code throws an error when I enter such a value

                  The code throws an error? At runtime? Don't you mean the compiler throws an error on this? 08246 as a C++ literal is octal, because of the leading 0, and then 8 is not a valid digit in base 8!

                  If you want to retain a leading 0, so it's not "really" "just" a number, that is harder (also in a QSpinBox if you changed to use that, though still doable). As it is, this is why @J-Hilk's code shows how to achieve it.

                  1 Reply Last reply
                  2
                  • 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

                                          • Login

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