当前位置:网站首页>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));
}
}
边栏推荐
- Student course selection information management system based on ssm+jsp framework [source code + database]
- [USACO05JAN]Watchcow S(欧拉回路)
- 操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
- 题解:《压缩技术》(原版、续集版)
- D如何检查null
- Pointer from entry to advanced (1)
- Unity small map production [2]
- Unity skframework framework (XIII), question module
- Dingtalk 发送消息
- Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
猜你喜欢

Unity skframework framework (XVI), package manager development kit Manager

运维必备——ELK日志分析系统

Node.js通过ODBC访问PostgreSQL数据库

Chinese name extraction (toy code - accurate head is too small, right to play)

Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm

Explanation: here is your UFO, Goldbach conjecture

题解:《压缩技术》(原版、续集版)

Daily practice of C language --- monkeys divide peaches

Answer: can the audio be set to on by default during easydss video on demand?

Node. JS accessing PostgreSQL database through ODBC
随机推荐
D为何链接不了dll
D如何检查null
Why can't d link DLL
De4000h storage installation configuration
[cloud native database] what to do when encountering slow SQL (Part 1)?
JS逆向之巨量创意signature签名
题解:《你的飞碟在这儿》、《哥德巴赫猜想》
【模板】最长公共子序列 (【DP or 贪心】板子)
三谈exception——错误处理
A better database client management tool than Navicat
Why is the default of switch followed by break?
2022零代码/低代码开发白皮书【伙伴云出品】附下载
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
On flow delivery between microservices
D how to check null
Memory management 01 - link script
研究表明“气味相投”更易成为朋友
Research shows that "congenial" is more likely to become friends
How to use SAP's metadata framework (MDF) to build custom business rules?
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)





.
