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. Simple calculation using line edit
Forum Updated to NodeBB v4.3 + New Features

Simple calculation using line edit

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.5k 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.
  • C Offline
    C Offline
    croussou
    wrote on 31 Jan 2012, 19:06 last edited by
    #1

    Hi there,

    I would like to calculate two values using the same line edit. I know how to calculate the values using two line edit input widgets, simply by converting the text into an integer or a double and storing the values into the corresponding variables. What I would like to do is read a number from line edit, then click the addition sign, the line edit will be cleared and ready to accept the second value.

    @
    void Calc::Addition()
    {
    QString str1 = ui.lineEditNumber1->text();
    ui.lineEditNumber1->clear();
    QString str2 = ui.lineEditNumber1->text();

     bool check;
     num1 = str1.toInt(&check);
     num2 = str2.toInt(&check);
     res = num1 + num2;
    

    }
    @

    Certainly now the result is always equal to the first number because the second number is always 0...

    Any help would be appreciated.

    Thank you in advance.

    Kind Regards,

    croussou

    Regards,

    croussou

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KillGabio
      wrote on 31 Jan 2012, 19:14 last edited by
      #2

      well you have to add a toolbutton that triggers an action that stores the value in a variable...for example

      @private slots:
      void on_plus_clicked(){
      res += ui.lineEditNumber1->text().toInt ();
      ui.lineEditNumber1->clear ();
      }@

      1 Reply Last reply
      0
      • C Offline
        C Offline
        croussou
        wrote on 31 Jan 2012, 19:31 last edited by
        #3

        Works perfectly, thanks a bunch. I guess the same concept applies for the rest of the functions...

        Regards,

        croussou

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KillGabio
          wrote on 31 Jan 2012, 19:50 last edited by
          #4

          Yes, it`s always the same concept. Glad it helped you.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            croussou
            wrote on 13 Feb 2012, 19:31 last edited by
            #5

            I went through couple of demo calculator apps and all of them seem to have a single line edit handling all the operations. In addition to that, the line edit seems divided in half since while the different operations take place you can merely see the result at a specific time, unless you hit the "=" key which somehow terminates the calculations and displays the final result. Similar to Windows default calculator...

            Can someone please explain how can I possibly add that to my calculator app as well?

            Thank you in advance.

            Regards,

            croussou

            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