当前位置:网站首页>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));
}
}
边栏推荐
- Android kotlin broadcast technology point
- Unity skframework framework (XII), score scoring module
- Let juicefs help you with "remote backup"
- [indomitable medal activity] life goes on and writing goes on
- How to use SAP's metadata framework (MDF) to build custom business rules?
- Subcontracting configuration of uniapp applet subpackages
- Dingtalk 发送消息
- P1347 排序(拓扑 + spfa判断环 or 拓扑[内判断环])
- Pocket Raider comments
- Don't spend money, spend an hour to build your own blog website
猜你喜欢
题解《子数整数》、《欢乐地跳》、《开灯》
Subcontracting configuration of uniapp applet subpackages
How to explain binary search to my sister? This is really difficult, fan!
Student course selection information management system based on ssm+jsp framework [source code + database]
为什么switch 的default后面要跟break?
BeanUtils--浅拷贝--实例/原理
Solve "sub number integer", "jump happily", "turn on the light"
Téléchargement par navigateur
Browser driven Download
Halcon extract orange (Orange)
随机推荐
[Unity]使用GB2312,打包后程序不正常解决方案
【OpenGL】笔记二十九、高级光照(镜面高光)
[cloud native database] what to do when encountering slow SQL (Part 1)?
Will your sleep service dream of the extra bookinfo on the service network
[youcans' image processing learning course] general contents
Memory management 01 - link script
Daily question: 1175 Prime permutation
Qt如何设置固定大小
2022 Heilongjiang provincial examination on the writing skills of Application Essays
题解《子数整数》、《欢乐地跳》、《开灯》
Student course selection information management system based on ssm+jsp framework [source code + database]
Detailed collection of common MySQL commands
numpy数组计算
错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”
Dingtalk 发送消息
Unity skframework framework (XII), score scoring module
Redis database persistence
Sum of the first n terms of Fibonacci (fast power of matrix)
693. Travel sequencing (map + topology)
[document tree, setting] font becomes smaller