I have a problem with managing to code the simple calculator. I have two QLineEdits, which I want to connect together, do simple calculations as addition, multiplication, and then show the result in the third QLineEdit, as shown in this picture.
Asked
Active
Viewed 304 times
0
-
What is exactly your doubt? Can you attach your code, please? – DYangu May 29 '16 at 22:31
-
I want to do a calculator with different calculations but to the point. The user of a program writes for example how much he weights and how tall he is, as a result we get a BMI in the 3rd QLineEdit. And I don't really know now how and where put the necessary code. How to link them to read the variables, calculate them and show the result in the end. – May 29 '16 at 22:37
-
No, I just wanted to ask what is the best way of achieving it. I can't figure out how to use the slots and signals in this case. – May 29 '16 at 23:07
1 Answers
2
I think, the best components for such task are QDoubleSpinBox
- http://doc.qt.io/qt-5/qdoublespinbox.html (for float and double values) or QSpinBox
- http://doc.qt.io/qt-5/qspinbox.html (for integers values). Add button with name "Addition" and connect slot on button signal void QAbstractButton::clicked(bool checked = false)
(http://doc.qt.io/qt-5/qabstractbutton.html#clicked).
Your form will be like this:
The slot connected to "Addition" button clicked signal will be like this:
void MainWindow::slotPushButtonAdditionClicked(bool checked)
{
Q_UNUSED(checked);
ui->doubleSpinBoxResult->setValue(
ui->doubleSpinBox1->value() +
ui->doubleSpinBox2->value());
}

Kirill Chernikov
- 1,387
- 8
- 21