Simple calculation using line edit



  • 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



  • 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 ();
    }@



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.