What I need to do is use data from 2 inputs (QLineEdit) and run this calculation in the background:
a - b = c
c * 2 = d / 0.9 = sum
So if I understand you correctly, your user will type a number into each line edit (a and b), and when she is done, your program will run the two numbers through the equations above and display the result in another line edit? Where does "d" come from? Do you mean:
d = c * 2
sum = d / 0.9
??
As far as retrieving the numbers from the line edits, that's simple (in C++; I don't know Python well enough to ensure my example would be correct):
void MyDataEntryForm::calculateAndDisplaySum()
{
double a = lineEditA->text().toDouble();
double b = lineEditB->text().toDouble();
double sum = ((a - b) * 2.0) / 0.9;
lineEditSum
->setText
( QString::number( sum,
'g',
2 ) );
// which will give a number like 'nn.nn'; change the 2 to something else for more decimals}
void MyDataEntryForm::calculateAndDisplaySum()
{
double a = lineEditA->text().toDouble();
double b = lineEditB->text().toDouble();
double sum = ((a - b) * 2.0) / 0.9;
lineEditSum->setText( QString::number( sum, 'g', 2 ) ); // which will give a number like 'nn.nn'; change the 2 to something else for more decimals
}
To copy to clipboard, switch view to plain text mode
You have at least two options for getting the numbers from line edits A and B:
- An Excel-like mode, where as soon as either number changes, line edit Sum gets updated
- A "batch" mode, where the user has to click an "update" or "calculate" button before the sum gets updated
If your users are used to Excel, this might be a more natural mode of use. If you implement the batch mode option, then you do nothing when the values are edited, but you connect the "clicked()" signal from your calculate pushbutton to a slot that executes the code above:
MyDataEntryForm::MyDataEntryForm() : ui( new Ui::MyDataEntryForm )
{
ui->setupUi( this );
connect( ui->calculateBtn, SIGNAL( clicked() ), this, SLOT( onCalculateClicked() ) );
}
void MyDataEntryForm::onCalculateClicked()
{
calculateAndDisplaySum();
}
MyDataEntryForm::MyDataEntryForm() : ui( new Ui::MyDataEntryForm )
{
ui->setupUi( this );
connect( ui->calculateBtn, SIGNAL( clicked() ), this, SLOT( onCalculateClicked() ) );
}
void MyDataEntryForm::onCalculateClicked()
{
calculateAndDisplaySum();
}
To copy to clipboard, switch view to plain text mode
If you use the Excel mode, then it is a bit more complicated, since you need to monitor the A and B line edits for changes and do the calculation dynamically:
MyDataEntryForm::MyDataEntryForm() : ui( new Ui::MyDataEntryForm )
{
ui->setupUi( this );
connect( ui->lineEditA, SIGNAL( editingFinished() ), this, SLOT( onEditingFinished ) );
connect( ui->lineEditB, SIGNAL( editingFinished() ), this, SLOT( onEditingFinished ) );
}
void MyDataEntryForm::onEditingFinished()
{
// We don't care which line edit changed, since we do the same thing for each of them
calculateAndDisplaySum();
}
MyDataEntryForm::MyDataEntryForm() : ui( new Ui::MyDataEntryForm )
{
ui->setupUi( this );
connect( ui->lineEditA, SIGNAL( editingFinished() ), this, SLOT( onEditingFinished ) );
connect( ui->lineEditB, SIGNAL( editingFinished() ), this, SLOT( onEditingFinished ) );
}
void MyDataEntryForm::onEditingFinished()
{
// We don't care which line edit changed, since we do the same thing for each of them
calculateAndDisplaySum();
}
To copy to clipboard, switch view to plain text mode
Now, none of this code has any error checking. What if the user enters "tater tots" instead of "42.0" in one of the line edits? So you could get fancier. If there are prescribed allowed ranges for A or B, you could install QDoubleValidator instances on each line edit to ensure that the user can't enter anything other than a valid number. If you install a validator, the editingFinished() signal will not be emitted unless a valid value is entered, so you can be assured that by the time it gets to your calculation, everything is valid.
This will work with either interaction style - but you will need to manually check the validator state using the input text if you use the pushbutton style of interaction. I don't remember if the validator will allow an invalid entry to be made in the line edit; what I do remember is that the editingFinished() signal isn't emitted unless the input is valid, so this is a safer way to ensure correct inputs.
As for your last question about converting a number in a range to a fixed value, if you have a predetermined set of ranges and outputs, the simplest way is just an if/else construct:
double output = 0.0;
if ( 0.00 <= input && input < 0.20 ) // what if the input is between 0.19 and 0.20? Your equations don't cover those corner cases. This does.
output = 5.0;
else if ( 0.20 <= input && input < 0.50 )
output = 10.0;
else if ( 0.50 <= input && input < 0.80 )
output = 15.0;
else if ( 0.80 <= input && input < 1.10 )
output = 20.0;
else
{
// error, so take appropriate action
}
double output = 0.0;
if ( 0.00 <= input && input < 0.20 ) // what if the input is between 0.19 and 0.20? Your equations don't cover those corner cases. This does.
output = 5.0;
else if ( 0.20 <= input && input < 0.50 )
output = 10.0;
else if ( 0.50 <= input && input < 0.80 )
output = 15.0;
else if ( 0.80 <= input && input < 1.10 )
output = 20.0;
else
{
// error, so take appropriate action
}
To copy to clipboard, switch view to plain text mode
Good luck.
Bookmarks