废话不多说直接放图(左为Win10原版计算器,右为手搓计算器)
全部功能肯定没那么容易做,也只能整整基本的算术运算,当然有点小BUG也是正常的,因为原版的逻辑判断有点多,关系到各种状态,希望你们有兴趣的可以自己完善下,文件已打包可下载。
链接:计算器下载
提取码:nisy
widget.h
#if 1
#ifndef WIDGET_H
#define WIDGET_H
#include <QLabel>
#include <QWidget>
#include <QPushButton>
#include <QGraphicsEffect>
class Widget:public QWidget{
Q_OBJECT
public:
Widget(QWidget* parent=0);
~Widget();
private slots:
void Action(bool);//按钮响应动作
private:
#define WIDGET_WIDTH 322
#define WIDGET_HEIGHT 502
#define BUTTON_WIDTH 75
#define BUTTON_HEIGHT 49
void CreateUI();//创建UI
QLabel* labLowerLabel;//上面的标签
QLabel* labUpperLabel;//下面的标签
QString* strOperator;//运算符号
QString* strUpperValue;//上面的数值
QString* strLowerValue;//下面的数值
QPushButton* btnCalculationButton[20];//计算按钮
QGraphicsDropShadowEffect* efeCalculationButtonShadow[20];//计算按钮的阴影
};
#endif // WIDGET_H
#endif
widget.cpp
#include "widget.h"
Widget::Widget(QWidget* parent):QWidget(parent){
CreateUI();
}
Widget::~Widget(){
}
void Widget::CreateUI(){
this->setWindowOpacity(1.0);
this->setWindowTitle("计算器");
this->resize(WIDGET_WIDTH, WIDGET_HEIGHT);
this->setFixedSize(WIDGET_WIDTH, WIDGET_HEIGHT);
this->strOperator = new QString("");
this->strUpperValue = new QString("");
this->strLowerValue = new QString("0");
this->labLowerLabel = new QLabel(this);
this->labLowerLabel->setText("0");
this->labLowerLabel->setGeometry(5, 78, 310, 49);
this->labLowerLabel->setAlignment(Qt::AlignRight);
this->labLowerLabel->setFont(QFont("Microsoft YaHei", 33));
this->labUpperLabel = new QLabel(this);
this->labUpperLabel->setText("");
this->labUpperLabel->setStyleSheet("color:rgb(97,97,97);");
this->labUpperLabel->setGeometry(5, 54, 310, 49);
this->labUpperLabel->setAlignment(Qt::AlignRight);
this->labUpperLabel->setFont(QFont("Microsoft YaHei", 10));
QString strCharacter = "%,C,⇍,÷,7,8,9,×,4,5,6,-,1,2,3,+,+/-,0,.,=";
QStringList strCharacterList = strCharacter.split(",");
QString strButtonStyleSheet = "QPushButton{background-color:rgb(249,249,249);"
" border:none;border-radius:4px;"
"}"
"QPushButton:hover{"
" background-color:rgb(229,229,229);"
"}"
"QPushButton:pressed{"
" background-color:rgb(209,209,209);"
"}";
QString strOperatorButtonStyleSheet = "QPushButton{"
" background-color:rgb(51,51,51);"
" color:rgb(255,255,255);"
" border:none;"
" border-radius:4px;"
"}"
"QPushButton:hover{"
" background-color:rgb(61,61,61);"
"}"
"QPushButton:pressed{"
" background-color:rgb(71,71,71);"
"}";
int ID = 0;
for(int i = 0; i < 5; i++){
for(int j = 0; j < 4; j++){
btnCalculationButton[ID] = new QPushButton(this);
btnCalculationButton[ID]->setText(strCharacterList[ID]);
efeCalculationButtonShadow[ID] = new QGraphicsDropShadowEffect(this);
efeCalculationButtonShadow[ID]->setOffset(0,0);
efeCalculationButtonShadow[ID]->setBlurRadius(3);
efeCalculationButtonShadow[ID]->setColor(QColor(129,129,129,210));
btnCalculationButton[ID]->setGraphicsEffect(efeCalculationButtonShadow[ID]);
btnCalculationButton[ID]->setGeometry(5 + j * (BUTTON_WIDTH + 4), 236 + i * (BUTTON_HEIGHT + 4), BUTTON_WIDTH, BUTTON_HEIGHT);
if(j == 3){
btnCalculationButton[ID]->setFont(QFont("Microsoft YaHei", 16));
}else{
btnCalculationButton[ID]->setFont(QFont("Microsoft YaHei", 13));
}
if(ID != 19){
btnCalculationButton[ID]->setStyleSheet(strButtonStyleSheet);
}else{
btnCalculationButton[ID]->setStyleSheet(strOperatorButtonStyleSheet);
}
QObject::connect(btnCalculationButton[ID], SIGNAL(clicked(bool)), this, SLOT(Action(bool)));
ID++;
}
}
}
void Widget::Action(bool){
QPushButton* btnResponseButton = dynamic_cast<QPushButton*>(sender());
QString strResponseButtonText = btnResponseButton->text();
bool bRenovate = false;
if( (((strResponseButtonText >= "0") && (strResponseButtonText <= "9")) || (strResponseButtonText == ".")) && (this->strLowerValue->size() < 10) ){
if(this->strLowerValue->size() < 2){
if(strResponseButtonText >= "0" && *this->strLowerValue < "1"){
this->strLowerValue->clear();
}
*this->strLowerValue += strResponseButtonText;
}else if( strLowerValue->contains(".", Qt::CaseInsensitive)){
if(strResponseButtonText != "."){
*this->strLowerValue += strResponseButtonText;
}
}else{
*this->strLowerValue += strResponseButtonText;
}
}else if( (strResponseButtonText == "+") || (strResponseButtonText == "-") || (strResponseButtonText == "×") || (strResponseButtonText == "÷") ||
(strResponseButtonText == "%") || (strResponseButtonText == "=") ){
if(!this->strUpperValue->isEmpty()){
double dSumValue = (*(this->strUpperValue)).toDouble();
if(strResponseButtonText != "="){
if(*this->strOperator == "+"){
dSumValue += (*(this->strLowerValue)).toDouble();
}else if(*this->strOperator == "-"){
dSumValue -= (*(this->strLowerValue)).toDouble();
}else if(*this->strOperator == "×"){
dSumValue *= (*(this->strLowerValue)).toDouble();
}else if(*this->strOperator == "÷"){
dSumValue /= (*(this->strLowerValue)).toDouble();
}else if(*this->strOperator == "%"){
dSumValue = (int)dSumValue % (*(this->strLowerValue)).toInt();
}
bRenovate = true;
*this->strLowerValue = "0";
*this->strOperator = strResponseButtonText;
*this->strUpperValue = QString::number(dSumValue);
this->labLowerLabel->setText(*this->strUpperValue);
this->labUpperLabel->setText(*this->strUpperValue + strResponseButtonText);
}else{
if(*this->strOperator != "="){
this->labUpperLabel->setText(*this->strUpperValue + *this->strOperator + *this->strLowerValue + strResponseButtonText);
if(*this->strOperator == "+"){
dSumValue += (*(this->strLowerValue)).toDouble();
}else if(*this->strOperator == "-"){
dSumValue -= (*(this->strLowerValue)).toDouble();
}else if(*this->strOperator == "×"){
dSumValue *= (*(this->strLowerValue)).toDouble();
}else if(*this->strOperator == "÷"){
dSumValue /= (*(this->strLowerValue)).toDouble();
}else if(*this->strOperator == "%"){
dSumValue = (int)dSumValue % (*(this->strLowerValue)).toInt();
}
bRenovate = true;
this->strOperator->clear();
this->strUpperValue->clear();
*this->strLowerValue = QString::number(dSumValue);
this->labLowerLabel->setText(*this->strLowerValue);
}else{
bRenovate = true;
*this->strOperator = strResponseButtonText;
*this->strUpperValue = *this->strLowerValue;
this->labUpperLabel->setText(*this->strUpperValue + strResponseButtonText);
*this->strLowerValue = "0";
}
}
}else{
bRenovate = true;
*this->strOperator = strResponseButtonText;
*this->strUpperValue = *this->strLowerValue;
this->labUpperLabel->setText(*this->strUpperValue + strResponseButtonText);
*this->strLowerValue = "0";
}
}else if(strResponseButtonText == "C"){
*this->strLowerValue = "0";
this->strUpperValue->clear();
this->strOperator->clear();
this->labUpperLabel->setText(*this->strUpperValue);
}else if(strResponseButtonText == "⇍"){
if(this->strLowerValue->size() > 1){
this->strLowerValue->chop(1);
}else{
*this->strLowerValue = "0";
}
}else if(strResponseButtonText == "+/-"){
if((*(this->strLowerValue)).toDouble() > 0){
*this->strLowerValue = "-" + *this->strLowerValue;
}else{
this->strLowerValue->remove(0, 1);
}
}
if(!bRenovate){
this->labLowerLabel->setText(*this->strLowerValue);
}
}
Main.cpp
#include <QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc,argv);
Widget* win = new Widget();
win->show();
return a.exec();
}