当前位置:网站首页>QT - make a simple calculator - realize four operations
QT - make a simple calculator - realize four operations
2022-07-02 13:48:00 【Across mountains and rivers, across the sea】
Qt- Make a simple calculator - Realize four operations
Realization effect
Realization Add
Realization reduce
Realization ride
Realization except
How to complete ?
On the basis of the previous chapter , Delete +, Change to
This .
choice Editing project , Pictured.
Install the plus sign in the figure below , add to + ,- , * , / arithmetic
Code section

Complete code
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void calSlot();
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp( Key code )
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(
//ui Inside calButton object ( Calculate button )
ui->calButton,
// Send a signal ( Click the signal )
SIGNAL(clicked()),
// Give the current object
this,
// With a slot function calSlot() To connect
SLOT(calSlot()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::calSlot()
{
int first=ui->firstLineEdit->text().
toInt();
int second=ui->secondLineEdit->text().
toInt();
int result;
if(ui->comboBox->currentIndex()==0){
//+
result=first+second;
ui->resultLineEdit->setText(QString::number(result));
}
if(ui->comboBox->currentIndex()==1){
//-
result=first-second;
ui->resultLineEdit->setText(QString::number(result));
}
if(ui->comboBox->currentIndex()==2){
//*
result=first*second;
ui->resultLineEdit->setText(QString::number(result));
}
if(ui->comboBox->currentIndex()==3){
// /
if(second==0)return;
result=first/second;
ui->resultLineEdit->setText(QString::number(result));
}
}
边栏推荐
- 不会看器件手册的工程师不是个好厨子
- (POJ - 1984) navigation nightare (weighted and search set)
- 验证失败,请检查您的回电网址。您可以按照指导进行操作
- Achievements in science and Technology (27)
- uniapp小程序 subPackages分包配置
- JS reverse massive creative signature
- [Unity]使用GB2312,打包后程序不正常解决方案
- 题解:《压缩技术》(原版、续集版)
- 科技的成就(二十七)
- Find love for speed in F1 delta time Grand Prix
猜你喜欢

Partner cloud form strong upgrade! Pro version, more extraordinary!

混沌工程平台 ChaosBlade-Box 新版重磅发布

Redis数据库持久化

题解:《你的飞碟在这儿》、《哥德巴赫猜想》

Daily practice of C language --- monkeys divide peaches

Code implementation MNLM
![Student course selection information management system based on ssm+jsp framework [source code + database]](/img/71/900d83dba41974589b15d23e632119.png)
Student course selection information management system based on ssm+jsp framework [source code + database]

ensp简单入门
![[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials](/img/42/21f6d0fdd159faa8b63713624c95a2.png)
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials

Solve "sub number integer", "jump happily", "turn on the light"
随机推荐
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
P1347 排序(拓扑 + spfa判断环 or 拓扑[内判断环])
[Blue Bridge Cup] children's worship circle
Just 1000 fans, record it
OpenApi-Generator:简化RESTful API开发流程
How to explain binary search to my sister? This is really difficult, fan!
qt中uic的使用
What are eNB, EPC and PGW?
uniapp小程序 subPackages分包配置
P3008 [USACO11JAN]Roads and Planes G (SPFA + SLF优化)
A better database client management tool than Navicat
Dingtalk send message
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
Why is the default of switch followed by break?
Three methods of finding LCA of the nearest common ancestor
Node. JS accessing PostgreSQL database through ODBC
Redis database persistence
OpenFOAM:lduMatrix&lduAddressing
Three talking about exception -- error handling
De4000h storage installation configuration





.
