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