Calculator using Qt

In this post, you will learn how to make a simple calculator using Qt creator.

Steps:

  1. Open Qt Creator Click on File from the menu and click on New File or Project…(Ctrl +  N).
  2. From the left column select Application, from the middle column select Qt Widget Application.
  3. Click on Choose.
  4. Give a name to the project and set the path where you want to save this project files.
  5. Click Next,
  6. Default kits will be selected.
  7. Click next.
  8. Give the class name of your choice. I gave Calculator.
  9. Select Baseclass as QMainWindow from the dropdown list.
  10. (Generate form box can be unchecked as we are not going to use it, we will be creating UI with code only).
  11. Click Next
  12. Click on Finish.

You will see project files., click on the right arrow from the left panel, if you don`t see the below-mentioned files.

Now we will be adding codes to make UI and add functionality.

main.cpp doesn`t need to be modified.

Open calculator.h

and made the following changes.

#ifndef CALCULATOR_H
#define CALCULATOR_H

#include <QMainWindow>

namespace Ui {
class Calculator;
}

class Calculator : public QMainWindow
{
    Q_OBJECT

public:
    explicit Calculator(QWidget *parent = nullptr);
    ~Calculator();

    QString answerString;
    int anserNum;
    QString str;
    int ansInFormatFlag = 0;
    bool formatFlag = false;
    void setTextToInputLineEdit(QObject *);
private:
    Ui::Calculator *ui;
private slots:
    void inputChanged();
    void BinaryOctalHex();
};

#endif // CALCULATOR_H

Open calculator.cpp

Made changes as mentioned below.

Click on Build from the menu and click on Run. (Ctrl + R) to test the app.

Download Project files

Video

Thank you for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *