In this post, you will learn how to make a simple calculator using Qt creator.
Steps:
- Open Qt Creator Click on File from the menu and click on New File or Project…(Ctrl + N).
- From the left column select Application, from the middle column select Qt Widget Application.
- Click on Choose.
- Give a name to the project and set the path where you want to save this project files.
- Click Next,
- Default kits will be selected.
- Click next.
- Give the class name of your choice. I gave Calculator.
- Select Baseclass as QMainWindow from the dropdown list.
- (Generate form box can be unchecked as we are not going to use it, we will be creating UI with code only).
- Click Next
- 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.
Restricted content. Please login to view the content.
Click on Build from the menu and click on Run. (Ctrl + R) to test the app.
Download Project files
Restricted content. Please login to view the content.
Video
Thank you for reading.