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. Qt How to run a calculation when line edit box has data input and calculate button is click
QtWS25 Last Chance

Qt How to run a calculation when line edit box has data input and calculate button is click

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 3.6k Views
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #3

    Hi and welcome to devnet,

    in on_pushButton_clicked, just retrieve the values from your input widgets (if needed convert them to int/float/double) and do the Math there. Once that is done, update the answer QLineEdit with your calculated value.

    Hope it helps

    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
    • B Offline
      B Offline
      BokJr
      wrote on last edited by
      #4

      I'm sorry but I'm extremely new to Qt programming and programming as a whole, are you able to break it down more simply?

      how do I retrieve the values from the input widgets? and how do i do the math for it in code after that?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Saugglocke
        wrote on last edited by
        #5

        Hi,

        you can access your input widgets with ui.
        Example:
        @
        void Induced_Prism::on_pushButton_clicked()
        {
        // get input values:
        float f = ui->inputWidget->text().toFloat();
        // calculate something
        // ...
        // update output lineEdit or what so ever:
        ui->outputLineEdit->setText(QString::number(f));
        }@

        Hope that helps, best regards

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BokJr
          wrote on last edited by
          #6

          that does help, exceptionally! however, i don't know how to "calculate something" or update output line edit

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Saugglocke
            wrote on last edited by
            #7

            Hm, of course you know? You posted the formula in your initial post. Just get all the values you need, as shown, then calculate it and then write it back to the outputLineEdit in your case it would be the lineEdit next to "Induced Prism" as far as I can tell.

            Otherwise I might be missing the point.

            Best regards

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BokJr
              wrote on last edited by
              #8

              I mean I don't know how to translate my equations to the code language

              1 Reply Last reply
              0
              • B Offline
                B Offline
                Binary91
                wrote on last edited by
                #9

                Which part of your calculations do you not understand?
                Simple calculations like adding, multiplying, dividing and so on are realized with simple keyboard use and valid types:
                @
                int i = 5, j = 10;
                int k = i + j, l = i*j, m = i-j, n = i/j;
                @
                For complex algorithms like your "sin alpha", just take a look at "Qt's math functions":http://qt-project.org/doc/qt-4.8/qtcore-qmath-h.html

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  BokJr
                  wrote on last edited by
                  #10

                  I understand my calculations fully, just not how to translate my calculation into Qt. because for instance the user will input certain value's into the following line edit boxes such as lineedit, lineedit_2, lineedit_3 and lineedit_4 then when the calculate pushbutton is clicked I want to calculate the difference between the numbers input from lineedit_3 and lineedit_4 for instance if the angle in line edit_3 is 75 degrees and the angle in line edit_4 is 180 the difference would be 105. Then I want to run the following calculation to reveal a calculated value to show up in Lineedit_5 = (sin 106)^2 * Lineedit_2 + Lineedit.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Saugglocke
                    wrote on last edited by
                    #11

                    Hey,

                    this should help:
                    @qreal angle = ui->lineedit_4->text().toDouble() - ui->lineedit_3->text().toDouble();
                    qreal Dc = ui->lineedit_2->text().toDouble();
                    qreal DS = ui->lineedit->text().toDouble();
                    qreal result = qPow(qSin(angle), 2) * Dc + DS;
                    ui->lineedit_5->setText(QString::number(result));@

                    bb

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      BokJr
                      wrote on last edited by
                      #12

                      excellent, thank you so much!

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        BokJr
                        wrote on last edited by
                        #13

                        Final quesiton I promise, after all your help I managed to write my own one after understanding how to input my calculations to Qt code.

                        So my final question is with lineedit7, this number needs to be returned as a positive value even if line edit_5 is a negative. how do I do that?

                        @void Induced_Prism::on_pushButton_clicked()
                        {
                        qreal angle = ui->lineEdit_4->text().toDouble() - ui->lineEdit_3->text().toDouble();
                        qreal Dc = ui->lineEdit_2->text().toDouble();
                        qreal DS = ui->lineEdit->text().toDouble();
                        qreal result = pow(sin(angle), 2) * Dc + DS;
                        ui->lineEdit_5->setText(QString::number(result));
                        }

                        void Induced_Prism::on_pushButton_2_clicked()
                        {
                        qreal C = ui->lineEdit_6->text().toDouble();
                        qreal F = ui->lineEdit_5->text().toDouble();
                        qreal result (C*F);
                        ui->lineEdit_7->setText(QString::number(result));
                        }
                        @

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Saugglocke
                          wrote on last edited by
                          #14

                          Use qAbs():

                          ui->lineEdit_7->setText(QString::number(qAbs(result)));

                          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