问题:通过代码完成一个10 以内加减法计算器。不需要自适应,界面固定360*350。
"=" 按钮90*140 ,其它按钮90*70。
参考样式
cpp
#define DEFULT_BUTTON_STYLE "\
QPushButton{\
color:#000000;\
border:1px solid #AAAAAA;\
border-radius:0;\
background-color:#FFFFFF;\
}\
QPushButton:pressed{\
color:#FFFFFF;\
background-color:#AAAAAA;\
}\
"
代码:
widget.h
cpp
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtWidgets>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
int num1; // 操作数1
int num2; // 操作数2
QString m_operator; // 运算符
QString current_input1; // 操作数1
QString current_input2; // 操作数2
int result; // 结果
int flag; // 是否已经输入运算符
};
#endif // WIDGET_H
widget.cpp
cpp
#include "widget.h"
#include "ui_widget.h"
#define DEFULT_BUTTON_STYLE "\
QPushButton{\
color:#000000;\
border:1px solid #AAAAAA;\
border-radius:0;\
background-color:#FFFFFF;\
}\
QPushButton:pressed{\
color:#FFFFFF;\
background-color:#AAAAAA;\
}\
"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("计算器");
current_input1.clear(); // 清空字符串
current_input2.clear();
flag = 0; // flag置0
QLabel *label = new QLabel; // 结果输出框label
label->setAlignment(Qt::AlignRight|Qt::AlignHCenter|Qt::AlignVCenter); // 靠右居中对齐
label->setText("0");
label->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
QPushButton *button[10];
for(int i = 0;i < 10;i++){
button[i] = new QPushButton;
button[i]->setText(QString::number(i));
button[i]->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
button[i]->setStyleSheet("DEFULT_BUTTON_STYLE");
connect(button[i],&QPushButton::clicked,this,[=](){
if(flag ==0){
current_input1 += QString::number(i); // 数字转换成字符串拼接
label->setText(current_input1); // 显示到label上
}
else{
current_input2 += QString::number(i); // 数字转换成字符串拼接
QString temp = current_input1 + m_operator + current_input2; // 字符串拼接
label->setText(temp); // 显示
}
});
}
QPushButton *add_button = new QPushButton;
add_button->setText("+");
add_button->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
connect(add_button,&QPushButton::clicked,this,[=](){
m_operator = "+";
flag = 1;
QString temp = current_input1 + m_operator; // 字符串拼接
label->setText(temp); // 显示
});
QPushButton *sub_button = new QPushButton;
sub_button->setText("-");
sub_button->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
connect(sub_button,&QPushButton::clicked,this,[=](){
m_operator = "-";
flag = 1;
QString temp = current_input1 + m_operator;
label->setText(temp); // 显示
});
QPushButton *equal_button = new QPushButton;
equal_button->setText("=");
equal_button->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
connect(equal_button,&QPushButton::clicked,this,[=](){
num1 = current_input1.toInt();
num2 = current_input2.toInt();
if(m_operator == "+")
result = num1 + num2;
else if(m_operator == "-")
result = num1 - num2;
QString temp = QString("%1 %2 %3 = %4").arg(num1).arg(m_operator).arg(num2).arg(result);
label->setText(temp); // 显示
current_input1.clear();
current_input2.clear();
num1 = 0;
num2 = 0;
flag = 0;
});
QPushButton *clear_button = new QPushButton;
clear_button->setText("C");
clear_button->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
connect(clear_button,&QPushButton::clicked,this,[=](){
current_input1.clear(); // 清空
current_input2.clear();
num1 = 0;
num2 = 0;
flag = 0;
label->setText("0"); // 显示到label上
});
QPushButton *del_button = new QPushButton;
del_button->setText("DEL");
del_button->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
connect(del_button,&QPushButton::clicked,this,[=](){
if(flag == 0){ // flag==0 说明目前处于第一个操作数
current_input1.chop(1); // 去除尾端数字
label->setText(current_input1);
}
else if(flag == 1 && current_input2.isEmpty()){ // 说明目前已经输入完运算符但还没有输入第二个操作数
m_operator =""; // 设置为空
flag = 0;
label->setText(current_input1); // 显示
}
else{ // 说明目前处于第二个操作数
current_input2.chop(1); // 去除尾端数字
QString temp = current_input1 + m_operator + current_input2;
label->setText(temp); // 显示
}
});
QGridLayout *layout = new QGridLayout(this); // 网格布局
layout->addWidget(label,0,0,1,4);
layout->addWidget(clear_button,1,0,1,1);
layout->addWidget(del_button,1,1,1,1);
layout->addWidget(add_button,1,2,1,1);
layout->addWidget(sub_button,1,3,1,1);
layout->addWidget(button[7],2,0,1,1);
layout->addWidget(button[8],2,1,1,1);
layout->addWidget(button[9],2,2,1,1);
layout->addWidget(button[0],2,3,1,1);
layout->addWidget(button[4],3,0,1,1);
layout->addWidget(button[5],3,1,1,1);
layout->addWidget(button[6],3,2,1,1);
layout->addWidget(button[1],4,0,1,1);
layout->addWidget(button[2],4,1,1,1);
layout->addWidget(button[3],4,2,1,1);
layout->addWidget(equal_button,3,3,2,1);
layout->setMargin(0); // 边界
layout->setSpacing(0); // 各部件之间间距
}
Widget::~Widget()
{
delete ui;
}
main.cpp
cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
输出:
初始化界面:
示例: