当前位置:网站首页>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));
}
}
边栏推荐
- De4000h storage installation configuration
- MAC (MacOS Monterey 12.2 M1) personal use PHP development
- 三翼鸟两周年:羽翼渐丰,腾飞指日可待
- Unity small map production [2]
- rxjs Observable 自定义 Operator 的开发技巧
- Clean up system cache and free memory under Linux
- Lucky numbers in the [leetcode daily question] matrix
- Partner cloud form strong upgrade! Pro version, more extraordinary!
- JS逆向之行行查data解密
- How to modify the error of easydss on demand service sharing time?
猜你喜欢
2、 Frame mode MPLS operation
Partner cloud form strong upgrade! Pro version, more extraordinary!
伙伴云表格强势升级!Pro版,更非凡!
Memory management 01 - link script
[OpenGL] notes 29. Advanced lighting (specular highlights)
混沌工程平台 ChaosBlade-Box 新版重磅发布
I did it with two lines of code. As a result, my sister had a more ingenious way
使用BLoC 构建 Flutter的页面实例
Research shows that "congenial" is more likely to become friends
de4000h存储安装配置
随机推荐
Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...
科技的成就(二十七)
Unity skframework framework (XIII), question module
ArrayList and LinkedList
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
D how to check null
Daily practice of C language --- monkeys divide peaches
Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
不会看器件手册的工程师不是个好厨子
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
运维必备——ELK日志分析系统
Unity skframework framework (XVI), package manager development kit Manager
中文姓名提取(玩具代码——准头太小,权当玩闹)
Uniapp develops wechat applet Tencent map function and generates sig signature of location cloud
693. 行程排序(map + 拓扑)
Unity skframework framework (XII), score scoring module
[USACO05JAN]Watchcow S(欧拉回路)