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



  • I am very very new to Qt and programming and have done everything so far from watching youtube clips and reading books, but I don't know how to get my application to run the following calculations when the calculate button pressed

    "https://www.flickr.com/gp/43133321@N04/TXM21f":

    the first calculation needs to work out "power on desired axis" using the following calculation;

    Off Axis Power = (sin alpha)^2 * Dc + DS, where

    a (alpha) = the angle between the main axis (106 in my pictures example) and desired meridian (180)
    Dc = cylinder
    DS = sphere

    Plot the values into the equation and you get: DT = (sin 106)^2 (-2.00) + (-1.00)

    the second calculation is to calculate the "induced prism" using the following calculation

    Induced Prism = Cf

    where C is the DEC (cm) - as per my photo
    where f is "power on desired axis"

    so Induced Prism = 0.2x2.84 for example



  • #include "induced_prism.h"
    #include "ui_induced_prism.h"

    Induced_Prism::Induced_Prism(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Induced_Prism)
    {
    ui->setupUi(this);

    }

    Induced_Prism::~Induced_Prism()
    {
    delete ui;
    }

    void Induced_Prism::on_pushButton_clicked()
    {
    }


  • Lifetime Qt Champion

    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



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



  • 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



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



  • 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



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



  • 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



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



  • 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



  • excellent, thank you so much!



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



  • Use qAbs():

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


Log in to reply
 

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