当前位置:网站首页>Qt-制作一个简单的计算器-实现四则运算
Qt-制作一个简单的计算器-实现四则运算
2022-07-02 10:18:00 【踏过山河,踏过海】
Qt-制作一个简单的计算器-实现四则运算
实现效果
实现 加
实现 减
实现 乘
实现 除
如何完成?
在上一章的基础上,删除+,改成
这个.
选择编辑项目,如图.
安下图的加号,添加+ ,- , * , / 四则运算
代码部分
完整代码
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(关键代码)
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(
//ui里面的calButton对象(计算按钮)
ui->calButton,
//发送一个信号(点击信号)
SIGNAL(clicked()),
//给当前这个对象
this,
//用一个槽函数calSlot()来连接
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));
}
}
边栏推荐
- JS逆向之行行查data解密
- Gee learning notes 2
- Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
- Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
- Web基础
- JS reverse row query data decryption
- Solve "sub number integer", "jump happily", "turn on the light"
- Solution: Compression Technology (original version and sequel version)
- Pointer from entry to advanced (1)
- 解答:EasyDSS视频点播时音频是否可以设置为默认开启?
猜你喜欢
Unity small map production [2]
Unity skframework framework (XIX), POI points of interest / information points
leetcode621. task scheduler
Performance optimization of memory function
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
Gee learning notes 2
Skillfully use SSH to get through the Internet restrictions
SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
de4000h存储安装配置
I did it with two lines of code. As a result, my sister had a more ingenious way
随机推荐
【OpenGL】笔记二十九、高级光照(镜面高光)
题解:《压缩技术》(原版、续集版)
验证失败,请检查您的回电网址。您可以按照指导进行操作
Redis database persistence
Quantum three body problem: Landau fall
【云原生数据库】遇到慢SQL该怎么办(上)?
石子合并板子【区间DP】(普通石子合并 & 环形石子合并)
【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解
题解《子数整数》、《欢乐地跳》、《开灯》
D如何检查null
Daily question: 1175 Prime permutation
2022零代码/低代码开发白皮书【伙伴云出品】附下载
Sum of the first n terms of Fibonacci (fast power of matrix)
Unity skframework framework (XX), VFX lab special effects library
Memory management 01 - link script
Detailed collection of common MySQL commands
Unity small map production [2]
Unity skframework framework (XIII), question module
rxjs Observable 自定义 Operator 的开发技巧
Solution: Compression Technology (original version and sequel version)