QT实现一个简单的计算器

时间:2022-04-15 10:14:46

最近几天在学习QT,在了解了信号和槽机制、布局管理、还有一些控件的使用后,便试着写了一个计算器,带自定义的快捷键,剪贴板(复制和粘贴)的操作,有 菜单栏->选择 里的功能还未实现,等啥时候有时间把它补上吧。还有许多不足和需要修改的地方,希望各位大牛能帮忙指出其中的问题,谢谢。

下面就贴出我的代码:

calcMainWindow.h:

#ifndef CALCMAINWINDOW_H
#define CALCMAINWINDOW_H

#include <QMainWindow>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QString>
#include <QVector>
#include <QKeyEvent>
#include <QLabel>
#include <QMenu>
#include <QAction>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

QString temp;

double number;
void operation(); //运算
void keyPressEvent(QKeyEvent *); //键盘响应
void creatAction(); //创建菜单栏选项
void creatMenu(); //创建菜单

private:
//菜单控件(功能尚未添加)
QMenu *chooseMenu; //“选择”菜单
QMenu *editMenu; //“编辑”菜单
QMenu *helpMenu; //“帮助”菜单

QAction *standerAction; //标准
QAction *scienceAction; //科学
QAction *historyAction; //历史记录
QAction *dateAction; //日期计算
QAction *copyAction; //复制
QAction *pasteAction; //粘贴
QAction *helpAction; //查看帮助
QAction *aboutAction; //关于

//计算器相关控件
QLabel *operationLabel;
QLabel *resultLabel;

QLineEdit *showTextEdit; //表达式显示栏
QLineEdit *resultTextEdit; //结果显示栏
QPushButton *backBtn; //回退
QPushButton *ceBtn; //清除
QPushButton *clearBtn; //清空
QPushButton *oneBtn;
QPushButton *twoBtn;
QPushButton *threeBtn;
QPushButton *fourBtn;
QPushButton *fiveBtn;
QPushButton *sixBtn;
QPushButton *sevenBtn;
QPushButton *eightBtn;
QPushButton *nineBtn;
QPushButton *zeroBtn;
QPushButton *pointBtn; //.
QPushButton *signBtn; //+/-

//四则运算
QPushButton *addBtn;
QPushButton *subBtn;
QPushButton *mulBtn;
QPushButton *divideBtn;
QPushButton *isBtn;

public slots:
void button_1_click();
void button_2_click();
void button_3_click();
void button_4_click();
void button_5_click();
void button_6_click();
void button_7_click();
void button_8_click();
void button_9_click();
void button_0_click();

void button_add_click();
void button_sub_click();
void button_mul_click();
void button_divide_click();
void button_is_click();

void button_cls_click();
void button_ce_click();
void button_back_click();
void button_point_click();
void button_sign_click();

void helpMessageBox();
void aboutMessageBox();

void copyResult();
void pasteNumber();
};

#endif // CALCMAINWINDOW_H

calcMainWindow.cpp:

#include "calcMainWindow.h"#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QRect>
#include <QMenuBar>
#include <QMessageBox>
#include <QClipboard>
#include <QApplication>

bool num2 = false; //判断是否只有一个数
int operator1 = 0; //运算符标记:+(1),-(2),*(3),/(4)
double result = 0; //结果
bool flag = false; //标记分母是否为0

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QWidget *centerWindow = new QWidget;
this->setCentralWidget(centerWindow);

creatAction();
creatMenu();

//标签
operationLabel = new QLabel("Operation:");
resultLabel = new QLabel("Result:");
//显示栏
showTextEdit = new QLineEdit;
showTextEdit->setReadOnly(true); //设为只读属性
resultTextEdit = new QLineEdit;
showTextEdit->setReadOnly(true);

//键盘按钮
backBtn = new QPushButton("←");
ceBtn = new QPushButton("CE");
clearBtn = new QPushButton("CLS");
oneBtn = new QPushButton("1");
twoBtn = new QPushButton("2");
threeBtn = new QPushButton("3");
fourBtn = new QPushButton("4");
fiveBtn = new QPushButton("5");
sixBtn = new QPushButton("6");
sevenBtn = new QPushButton("7");
eightBtn = new QPushButton("8");
nineBtn = new QPushButton("9");
zeroBtn = new QPushButton("0");
pointBtn = new QPushButton(".");
isBtn = new QPushButton("=");
isBtn->setStyleSheet("background-color: rgb(255,80,20)");
addBtn = new QPushButton("+");
subBtn = new QPushButton("-");
mulBtn = new QPushButton("*");
divideBtn = new QPushButton("/");
signBtn = new QPushButton("+/-");
//设置布局
QHBoxLayout *H0 = new QHBoxLayout;
H0->addWidget(operationLabel);
H0->addWidget(resultLabel);
H0->setStretch(0,2);
H0->setStretch(1,1);

QHBoxLayout *H1 = new QHBoxLayout;
H1->addWidget(showTextEdit);
H1->addWidget(resultTextEdit);
H1->setStretch(0,2);
H1->setStretch(1,1);

QHBoxLayout *H2 = new QHBoxLayout;
H2->addWidget(backBtn);
H2->addWidget(ceBtn);
H2->addWidget(clearBtn);
H2->addWidget(addBtn);

QHBoxLayout *H3 = new QHBoxLayout;
H3->addWidget(sevenBtn);
H3->addWidget(eightBtn);
H3->addWidget(nineBtn);
H3->addWidget(subBtn);

QHBoxLayout *H4 = new QHBoxLayout;
H4->addWidget(fourBtn);
H4->addWidget(fiveBtn);
H4->addWidget(sixBtn);
H4->addWidget(mulBtn);

QHBoxLayout *H5 = new QHBoxLayout;
H5->addWidget(oneBtn);
H5->addWidget(twoBtn);
H5->addWidget(threeBtn);
H5->addWidget(divideBtn);

QHBoxLayout *H6 = new QHBoxLayout;
H6->addWidget(signBtn);
H6->addWidget(zeroBtn);
H6->addWidget(pointBtn);
H6->addWidget(isBtn);

QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(H0);
mainLayout->addLayout(H1);
mainLayout->addLayout(H2);
mainLayout->addLayout(H3);
mainLayout->addLayout(H4);
mainLayout->addLayout(H5);
mainLayout->addLayout(H6);

connect(oneBtn, SIGNAL(clicked()), this, SLOT(button_1_click()));
connect(twoBtn, SIGNAL(clicked()), this, SLOT(button_2_click()));
connect(threeBtn, SIGNAL(clicked()), this, SLOT(button_3_click()));
connect(fourBtn, SIGNAL(clicked()), this, SLOT(button_4_click()));
connect(fiveBtn, SIGNAL(clicked()), this, SLOT(button_5_click()));
connect(sixBtn, SIGNAL(clicked()), this, SLOT(button_6_click()));
connect(sevenBtn, SIGNAL(clicked()), this, SLOT(button_7_click()));
connect(eightBtn, SIGNAL(clicked()), this, SLOT(button_8_click()));
connect(nineBtn, SIGNAL(clicked()), this, SLOT(button_9_click()));
connect(zeroBtn, SIGNAL(clicked()), this, SLOT(button_0_click()));

connect(addBtn, SIGNAL(clicked()), this, SLOT(button_add_click()));
connect(subBtn, SIGNAL(clicked()), this, SLOT(button_sub_click()));
connect(mulBtn, SIGNAL(clicked()), this, SLOT(button_mul_click()));
connect(divideBtn, SIGNAL(clicked()), this, SLOT(button_divide_click()));
connect(isBtn, SIGNAL(clicked()), this, SLOT(button_is_click()));
connect(signBtn, SIGNAL(clicked()), this, SLOT(button_sign_click()));

connect(clearBtn, SIGNAL(clicked()), this, SLOT(button_cls_click()));
connect(ceBtn, SIGNAL(clicked()), this, SLOT(button_ce_click()));
connect(backBtn, SIGNAL(clicked()), this, SLOT(button_back_click()));
connect(pointBtn, SIGNAL(clicked()), this, SLOT(button_point_click()));

centerWindow->setLayout(mainLayout);
}

MainWindow::~MainWindow()
{

}

/*
**创建菜单栏选项
*/
void MainWindow::creatAction()
{
standerAction = new QAction("&标准",this);
standerAction->setShortcut(tr("Alt+1")); //设置快捷键
standerAction->setStatusTip("标准计算器"); //设置状态栏

scienceAction = new QAction("&科学",this);
scienceAction->setShortcut(tr("Alt+2"));
scienceAction->setStatusTip("科学计算器");

historyAction = new QAction("&历史记录",this);
historyAction->setShortcut(tr("Alt+3"));
historyAction->setStatusTip("历史记录");

dateAction = new QAction("&日期计算",this);
dateAction->setShortcut(tr("Alt+4"));
dateAction->setStatusTip("日期计算");

copyAction = new QAction("&复制",this);
copyAction->setShortcut(tr("Ctrl+C"));
copyAction->setStatusTip("复制结果");
connect(copyAction, SIGNAL(triggered()), this, SLOT(copyResult()));

pasteAction = new QAction("&粘贴",this);
pasteAction->setShortcut(tr("Ctrl+V"));
pasteAction->setStatusTip("粘贴");
connect(pasteAction, SIGNAL(triggered()), this, SLOT(pasteNumber()));

helpAction = new QAction("&查看帮助",this);
helpAction->setShortcut(tr("F1"));
helpAction->setStatusTip("查看帮助");
connect(helpAction, SIGNAL(triggered()), this, SLOT(helpMessageBox()));

aboutAction = new QAction("&关于",this);
aboutAction->setShortcut(tr("F2"));
aboutAction->setStatusTip("关于");
connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutMessageBox()));

}

/*
**创建菜单栏
*/
void MainWindow::creatMenu()
{
//选择菜单
chooseMenu=menuBar()->addMenu("&选择(C)");
chooseMenu->addAction(standerAction); //添加按钮
chooseMenu->addAction(scienceAction);
chooseMenu->addSeparator(); //添加横线
chooseMenu->addAction(historyAction);
chooseMenu->addAction(dateAction);

//编辑菜单
editMenu=menuBar()->addMenu("&编辑(E)");
editMenu->addAction(copyAction);
editMenu->addAction(pasteAction);

//帮助菜单
helpMenu=menuBar()->addMenu("&帮助(H)");
helpMenu->addAction(helpAction);
helpMenu->addSeparator();
helpMenu->addAction(aboutAction);
}

/*
** 使用消息盒子创建关于窗口
*/
void MainWindow::aboutMessageBox()
{
QMessageBox::about(NULL, "关于", "计算器 V1.0 \r\n 2015.07.26");
}

void MainWindow::helpMessageBox()
{
QMessageBox::about(NULL, "帮助", "这儿没有任何帮助!");
}

void MainWindow::copyResult()
{
QClipboard *copy = QGuiApplication::clipboard(); //剪贴板操作
temp = QString::number(result);
copy->setText(temp); //复制到剪切板
}

void MainWindow::pasteNumber()
{
QClipboard *paste = QGuiApplication::clipboard();
temp = paste->text();
showTextEdit->setText(temp); //粘贴到剪切板
}

/*
**按下按钮后存入temp并在label上显示
*/
void MainWindow::button_1_click()
{
temp += "1";
showTextEdit->setText(temp);
}

void MainWindow::button_2_click()
{
temp += "2";
showTextEdit->setText(temp);
}

void MainWindow::button_3_click()
{
temp += "3";
showTextEdit->setText(temp);
}

void MainWindow::button_4_click()
{
temp += "4";
showTextEdit->setText(temp);
}

void MainWindow::button_5_click()
{
temp += "5";
showTextEdit->setText(temp);
}

void MainWindow::button_6_click()
{
temp += "6";
showTextEdit->setText(temp);
}

void MainWindow::button_7_click()
{
temp += "7";
showTextEdit->setText(temp);
}

void MainWindow::button_8_click()
{
temp += "8";
showTextEdit->setText(temp);
}

void MainWindow::button_9_click()
{
temp += "9";
showTextEdit->setText(temp);
}

void MainWindow::button_0_click()
{
bool ok;
double tem = temp.toDouble(&ok);

//无用的0不能输入
if(1 == temp.contains(".") || 0 != tem || NULL == temp)
{
temp += "0";
}
showTextEdit->setText(temp);
}

void MainWindow::button_add_click()
{
if(false == num2)
{
num2 = true;
}

operation();
operator1 = 1; //operator1用来存储运算符
showTextEdit->setText("+");
}

void MainWindow::button_sub_click()
{

if(false == num2)
{
num2 = true;
}

operation();
operator1 = 2;
showTextEdit->setText("-");
}

void MainWindow::button_mul_click()
{
if(false == num2)
{
num2 = true;
}

operation();
operator1 = 3;
showTextEdit->setText("*");
}

void MainWindow::button_divide_click()
{
if(false == num2)
{
num2 = true;
}

operation();
operator1 = 4;
showTextEdit->setText("/");
}

void MainWindow::operation()
{
bool ok;
if(NULL == temp)
{
number = 0;
}
else
{
number = temp.toDouble(&ok);
}

//通过operator1选择相应的运算
switch(operator1)
{
case 0:result = number;break;
case 1:result += number;break;
case 2:result -= number;break;
case 3:result *= number;break;
case 4:
if(number == 0)
{
flag = true;
break;
}
result /= number;
break;
default:
break;
}
temp = "";
}

void MainWindow::button_is_click()
{
bool ok;

//单个数字,无运算结果
if(num2 == false)
{
number = temp.toDouble(&ok);
QString str1 = QString::number(number);
resultTextEdit->setText(str1);
return;
}
operation();

num2 = false;
operator1 = 0;

if(true == flag)
{
resultTextEdit->setText("分母不能为0");
return;
}
temp = QString::number(result); //double转qstring
resultTextEdit->setText(temp);
}

void MainWindow::button_cls_click()
{
temp = "";
number = 0;
operator1 = 0;
result = 0;
showTextEdit->setText(temp);
resultTextEdit->setText(temp);
}

void MainWindow::button_ce_click()
{
temp = "";
showTextEdit->setText(temp);
}

void MainWindow::button_back_click()
{
temp.chop(1); //chop从字符串末尾删除1个字符
showTextEdit->setText(temp);
}

void MainWindow::button_point_click()
{
if(0 == temp.contains("."))
{
temp += ".";
}
showTextEdit->setText(temp);
}

void MainWindow::button_sign_click()
{
//判断是否位负数
if(temp.startsWith("-"))
{
temp = temp.mid(1);
showTextEdit->setText(temp);
return;
}
temp = temp.insert(0, "-");
showTextEdit->setText(temp);
}

void MainWindow::keyPressEvent(QKeyEvent *k)
{
//int b = k->key();
switch(k->key())
{
case Qt::Key_0:button_0_click();break;
case Qt::Key_1:button_1_click();break;
case Qt::Key_2:button_2_click();break;
case Qt::Key_3:button_3_click();break;
case Qt::Key_4:button_4_click();break;
case Qt::Key_5:button_5_click();break;
case Qt::Key_6:button_6_click();break;
case Qt::Key_7:button_7_click();break;
case Qt::Key_8:button_8_click();break;
case Qt::Key_9:button_9_click();break;
case Qt::Key_Plus:button_add_click();break; // +
case Qt::Key_Minus:button_sub_click();break; // -
case Qt::Key_Asterisk:button_mul_click();break; // *
case Qt::Key_Slash:button_divide_click();break; // /
case Qt::Key_Backspace:button_back_click();break; // ←
case Qt::Key_Enter:button_is_click();break; // =
case Qt::Key_Period:button_point_click();break; // .
default:break;
}
}

main.cpp:

#include "calcMainWindow.h"
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.setWindowTitle("灰熊问题的最优解");
    w.show();
 
    return a.exec();
}
 

转载请注明出处,新浪博客:灰熊问题的最优解