qt5实现计算器
程序员文章站
2022-03-15 21:13:07
...
(1)取出按钮中的字符
QString line = ui->pushButton1->text() //取出按钮pushButton1中的字符存放到line中
(2)设置输入框中显示的内容
ui->lineEdit->setText(line); //设置输入框lineEdit中显示的内容,line为要显示的内容
(3)将整形、浮点型、数值转换成字符
ui->lineEdit->setText(QString::number(sum)); //将浮点型转换成字符
(4)addwidgt说明:
//放置一个控件到一个单元格
void addWidget ( QWidget * widget, int row, int column, Qt::Alignment alignment = 0 )
//如果放置的控件超出一个单元格,则使用该方法
void addWidget ( QWidget * widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = 0 )
参数说明:
1)row:指放置控件的网格行号(行号从0开始);
2)colum:指放置控件的网格列号(从0开始);
3)alignment:对齐方式。
4)fromRow:指放置控件的起始网格行号;
5)fromColumn:指放置控件的起始网格列号;
6)rowSpan:指放置控件占多少行;
7)columnSpan:指放置控件占多少列。
计算器功能:对整数实现+、-、*、/编辑框的小功能
1,实现代码如下
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.setFixedSize(400,300);
w.setWindowTitle("计算器");
w.myLayout();
return a.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QLineEdit>
class MainWindow : public QMainWindow
{
Q_OBJECT
protected:
int num1,num2,result; //标记第一个数,第二个数和结果
int operat; //运算符号
int operat_flag;//标记是否输入符号
QLineEdit *data;
QPushButton *button_0;
QPushButton *button_1;
QPushButton *button_2;
QPushButton *button_3;
QPushButton *button_4;
QPushButton *button_5;
QPushButton *button_6;
QPushButton *button_7;
QPushButton *button_8;
QPushButton *button_9;
QPushButton *button_ce;
QPushButton *button_jia;
QPushButton *button_jian;
QPushButton *button_cheng;
QPushButton *button_chu;
QPushButton *button_result;
QString s;
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public:
void myLayout();
void operation();
void readNum(int num);
void init_data();
private slots:
void button_0_clicked();
void button_1_clicked();
void button_2_clicked();
void button_3_clicked();
void button_4_clicked();
void button_5_clicked();
void button_6_clicked();
void button_7_clicked();
void button_8_clicked();
void button_9_clicked();
void button_ce_clicked();
void button_result_clicked();
void button_cheng_clicked();
void button_chu_clicked();
void button_jia_clicked();
void button_jian_clicked();
};
#endif
mainwindow.cpp
#include "mainwindow.h"
#include <QPushButton>
#include <QLineEdit>
#include <QGridLayout>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}
void MainWindow::myLayout(){
QWidget *centerWindow = new QWidget;
this->setCentralWidget(centerWindow);
s = "";
init_data();
data = new QLineEdit("");
button_0 = new QPushButton("0");
button_1 = new QPushButton("1");
button_2 = new QPushButton("2");
button_3 = new QPushButton("3");
button_4 = new QPushButton("4");
button_5 = new QPushButton("5");
button_6 = new QPushButton("6");
button_7 = new QPushButton("7");
button_8 = new QPushButton("8");
button_9 = new QPushButton("9");
button_ce = new QPushButton("CE");
button_jia = new QPushButton("+");
button_jian = new QPushButton("-");
button_cheng = new QPushButton("*");
button_chu = new QPushButton("/");
button_result = new QPushButton("=");
data->setFixedHeight(30);
button_0->setMinimumHeight(50);
button_1->setMinimumHeight(50);
button_2->setMinimumHeight(50);
button_3->setMinimumHeight(50);
button_4->setMinimumHeight(50);
button_5->setMinimumHeight(50);
button_6->setMinimumHeight(50);
button_7->setMinimumHeight(50);
button_8->setMinimumHeight(50);
button_9->setMinimumHeight(50);
button_ce->setMinimumHeight(50);
button_jia->setMinimumHeight(50);
button_jian->setMinimumHeight(50);
button_cheng->setMinimumHeight(50);
button_chu->setMinimumHeight(50);
button_result->setMinimumHeight(50);
//连接信号
connect(button_0,SIGNAL(clicked(bool)),this,SLOT(button_0_clicked()));
connect(button_1,SIGNAL(clicked(bool)),this,SLOT(button_1_clicked()));
connect(button_2,SIGNAL(clicked(bool)),this,SLOT(button_2_clicked()));
connect(button_3,SIGNAL(clicked(bool)),this,SLOT(button_3_clicked()));
connect(button_4,SIGNAL(clicked(bool)),this,SLOT(button_4_clicked()));
connect(button_5,SIGNAL(clicked(bool)),this,SLOT(button_5_clicked()));
connect(button_6,SIGNAL(clicked(bool)),this,SLOT(button_6_clicked()));
connect(button_7,SIGNAL(clicked(bool)),this,SLOT(button_7_clicked()));
connect(button_8,SIGNAL(clicked(bool)),this,SLOT(button_8_clicked()));
connect(button_9,SIGNAL(clicked(bool)),this,SLOT(button_9_clicked()));
connect(button_jia,SIGNAL(clicked(bool)),this,SLOT(button_jia_clicked()));
connect(button_jian,SIGNAL(clicked(bool)),this,SLOT(button_jian_clicked()));
connect(button_cheng,SIGNAL(clicked(bool)),this,SLOT(button_cheng_clicked()));
connect(button_chu,SIGNAL(clicked(bool)),this,SLOT(button_chu_clicked()));
connect(button_ce,SIGNAL(clicked(bool)),this,SLOT(button_ce_clicked()));
connect(button_result,SIGNAL(clicked(bool)),this,SLOT(button_result_clicked()));
QGridLayout * layout = new QGridLayout;
layout->addWidget(data,1,1,1,4,Qt::Alignment()); //控件超出一个单元格,数据输出口
layout->addWidget(button_1,2,1,Qt::Alignment());
layout->addWidget(button_2,2,2,Qt::Alignment());
layout->addWidget(button_3,2,3,Qt::Alignment());
layout->addWidget(button_jia,2,4,Qt::Alignment());
layout->addWidget(button_4,3,1,Qt::Alignment());
layout->addWidget(button_5,3,2,Qt::Alignment());
layout->addWidget(button_6,3,3,Qt::Alignment());
layout->addWidget(button_jian,3,4,Qt::Alignment());
layout->addWidget(button_7,4,1,Qt::Alignment());
layout->addWidget(button_8,4,2,Qt::Alignment());
layout->addWidget(button_9,4,3,Qt::Alignment());
layout->addWidget(button_cheng,4,4,Qt::Alignment());
layout->addWidget(button_ce,5,1,Qt::Alignment());
layout->addWidget(button_0,5,2,Qt::Alignment());
layout->addWidget(button_result,5,3,Qt::Alignment());
layout->addWidget(button_chu,5,4,Qt::Alignment());
layout->setMargin(12);
centerWindow->setLayout(layout);
}
MainWindow::~MainWindow()
{
}
void MainWindow::init_data()
{
num1 = 0;
num2 = 0;
operat_flag = 0;
}
void MainWindow::readNum(int num)
{
//如果是第一个操作数
if(operat_flag == 0){
num1 = num1 * 10 + num;
}
//如果是第二个操作数
else{
num2 = num2 * 10 + num;
}
}
void MainWindow::operation()
{
switch(operat){
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
result = num1 / num2;
break;
}
//把结果存到num1中,清零num2
num1 = result;
num2 = 0;
s = QString::number(result,10);
}
void MainWindow::button_0_clicked(){
s += "0";
data->setText(s);
readNum(0);
}
void MainWindow::button_1_clicked(){
s += "1";
data->setText(s);
readNum(1);
}
void MainWindow::button_2_clicked(){
s += "2";
data->setText(s);
readNum(2);
}
void MainWindow::button_3_clicked(){
s += "3";
data->setText(s);
readNum(3);
}
void MainWindow::button_4_clicked(){
s += "4";
data->setText(s);
readNum(4);
}
void MainWindow::button_5_clicked(){
s += "5";
data->setText(s);
readNum(5);
}
void MainWindow::button_6_clicked(){
s += "6";
data->setText(s);
readNum(6);
}
void MainWindow::button_7_clicked(){
s += "7";
data->setText(s);
readNum(7);
}
void MainWindow::button_8_clicked(){
s += "8";
data->setText(s);
readNum(8);
}
void MainWindow::button_9_clicked(){
s += "9";
data->setText(s);
readNum(9);
}
void MainWindow::button_ce_clicked(){
s = "";
data->setText("");
//初始化数据
init_data();
}
void MainWindow::button_jia_clicked(){
s += "+";
data->setText(s);
operat_flag = 1;
operat = 1;
}
void MainWindow::button_jian_clicked(){
s += "-";
data->setText(s);
operat_flag = 1;
operat = 2;
}
void MainWindow::button_cheng_clicked(){
s += "*";
data->setText(s);
operat_flag = 1;
operat = 3;
}
void MainWindow::button_chu_clicked(){
s += "/";
data->setText(s);
operat_flag = 1;
operat = 4;
}
void MainWindow::button_result_clicked(){
operation();
QString a = QString::number(result,10);
data->setText(a);
}
完整代码连接:https://download.csdn.net/download/qq_40008325/11015502
上一篇: 刷脸支付取代二维码支付是必然趋势
下一篇: 【c语言】整数分解