当前位置:网站首页>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));
}
}
边栏推荐
- Why is the default of switch followed by break?
- Qt-制作一个简单的计算器-实现四则运算
- 【OpenGL】笔记二十九、高级光照(镜面高光)
- 验证失败,请检查您的回电网址。您可以按照指导进行操作
- 每日一题:1175.质数排列
- Android kotlin fragment technology point
- Verification failed, please check your call back website. You can follow the instructions
- Chinese name extraction (toy code - accurate head is too small, right to play)
- [cloud native database] what to do when encountering slow SQL (Part 1)?
- Drawing Nyquist diagram with MATLAB
猜你喜欢
Common options of tcpdump command: Three
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
Performance optimization of memory function
Téléchargement par navigateur
Pointer from entry to advanced (1)
Web Foundation
[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
Answer: can the audio be set to on by default during easydss video on demand?
Explanation: here is your UFO, Goldbach conjecture
能自动更新的万能周报模板,有手就会用!
随机推荐
错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”
[USACO05JAN]Watchcow S(欧拉回路)
Chaos engineering platform chaosblade box new heavy release
题解:《压缩技术》(原版、续集版)
Principle analysis of security rememberme
rxjs Observable 自定义 Operator 的开发技巧
MySQL -- convert rownum in Oracle to MySQL
为什么switch 的default后面要跟break?
numpy数组计算
D how to check null
石子合并板子【区间DP】(普通石子合并 & 环形石子合并)
(POJ - 1984) navigation nightare (weighted and search set)
Security RememberMe原理分析
Essential for operation and maintenance - Elk log analysis system
Solve "sub number integer", "jump happily", "turn on the light"
Unity small map production [2]
[document tree, setting] font becomes smaller
代码实现MNLM
运维必备——ELK日志分析系统
Why is the default of switch followed by break?