Search code examples
qtcalculatorqlineedit

How to link up calculations and QLineEdit


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.


Solution

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

    form

    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());
    }