1.1、计算器最终界面
这个计算器是仿window自带的计算器中的标准模式下的简单计算器,我只保留了简单计算用到的按钮,其余的按钮功能我已经去掉:
1.2、创建工程
请参考我前面写的这篇文章: (一)QT5之手动编写界面创建工程。
1.3、主要知识点
其实这一章用到的知识和前面那一章节一样,只是做了个升华,加多几个按钮来处理不同的事件而已,当然我们看到window自带的计算器界面相对看起来好看很多,所以这里涉及到新的知识就是关于按钮的style设置问题,其实这个也很简单,然后其余的就剩下逻辑的问题了。
1.4、关于按钮style配置
一下这个代码就是配置按钮的风格。
btn[cout + 2]->setStyleSheet("QPushButton{background-color:rgb(245,245,245);border:none;}QPushButton:hover{background-color:rgb(230,230,230);}QPushButton:pressed{background-color:rgb(180,180,180);}");
background-color:就是背景颜色
border:边框,这里我直接取消,这样就没有看到按钮被按下的效果。
QPushButton:hover:鼠标放置在按键上面时候显示的颜色。
QPushButton:pressed:这个是按键按下去显示的颜色。
目前只增加这个新的知识,所以下面我将我的代码贴出来,感兴趣的可以看一下。
1.5、代码
widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QDebug>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QLabel>
#include <QWidget>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void set_ui(void);
void calculator(void);
private slots:
void btn_handler(bool);
void delete_btn_handle(bool);
private:
QPushButton *btn[20];
QPushButton *btn_7;
QPushButton *btn_8;
QPushButton *btn_9;
QPushButton *btn_x;
QLabel *lab_last;
QLabel *lab_now;
QString dis_data;
int input_cout;
QString fist_str;
QString signed_str;
QString second_str;
QString result;
};
#endif // WIDGET_H
widget.cpp:
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
set_ui();
}
Widget::~Widget()
{
}
void Widget::set_ui(void)
{
this->resize(565,725);
this->setFixedSize(this->width(),this->height()); //设置窗体变化限定死
this->setWindowTitle("计算器");
this->setWindowOpacity(0.95); //设置窗体的不透明度 1.0完全不透明 0.0 完全透明
this->lab_now = new QLabel(this);
this->lab_last = new QLabel(this);
this->lab_last->setAlignment(Qt::AlignRight);
this->lab_last->setGeometry(10,30,545,50);
QFont lab_last_font( "Microsoft YaHei",15, 50);
this->lab_last->setFont(lab_last_font);
this->lab_last->setText("");
this->lab_now->setAlignment(Qt::AlignRight);
this->lab_now->setGeometry(10,80,545,100);
QFont lab_now_font( "Microsoft YaHei",30, 75);
this->lab_now->setFont(lab_now_font);
this->lab_now->setText("0");
QString str_list = "%,C,,÷,7,8,9,×,4,5,6,-,1,2,3,+,+/-,0,.,=";
QStringList btn_name_list= str_list.split(",");
//qDebug()<<"list:"<<btn_name_list;
//布局按钮
QFont fbtn_font( "Microsoft YaHei",25, 60);
QFont btn_font( "Microsoft YaHei",15, 60);
int cout = 0;
int btn_widht = 135;
int btn_height = 100;
for(int i = 0;i<5;i++){
cout = i* 4;
btn[cout + 0] = new QPushButton(this);
btn[cout + 0]->setText(btn_name_list[cout + 0]);
btn[cout + 0]->setStyleSheet("QPushButton{background-color:rgb(245,245,245);border:none;}QPushButton:hover{background-color:rgb(230,230,230);}QPushButton:pressed{background-color:rgb(180,180,180);}");
btn[cout + 0]->setFont(btn_font);
btn[cout + 0]->setGeometry(5,200 + i*(btn_height + 5),btn_widht,btn_height);
btn[cout + 1] = new QPushButton(this);
btn[cout + 1]->setText(btn_name_list[cout + 1]);
btn[cout + 1]->setStyleSheet("QPushButton{background-color:rgb(245,245,245);border:none;}QPushButton:hover{background-color:rgb(230,230,230);}QPushButton:pressed{background-color:rgb(180,180,180);}");
btn[cout + 1]->setFont(btn_font);
btn[cout + 1]->setGeometry(5 + (btn_widht + 5),200 + i*(btn_height + 5),btn_widht,100);
btn[cout + 2] = new QPushButton(this);
btn[cout + 2]->setText(btn_name_list[cout + 2]);
btn[cout + 2]->setStyleSheet("QPushButton{background-color:rgb(245,245,245);border:none;}QPushButton:hover{background-color:rgb(230,230,230);}QPushButton:pressed{background-color:rgb(180,180,180);}");
btn[cout + 2]->setFont(btn_font);
btn[cout + 2]->setGeometry(5 + (btn_widht + 5)*2,200 + i*(btn_height + 5),btn_widht,100);
btn[cout + 3] = new QPushButton(this);
btn[cout + 3]->setText(btn_name_list[cout + 3]);
btn[cout + 3]->setStyleSheet("QPushButton{background-color:rgb(245,245,245);border:none;}QPushButton:hover{background-color:rgb(230,230,230);}QPushButton:pressed{background-color:rgb(180,180,180);}");
btn[cout + 3]->setFont(btn_font);
btn[cout + 3]->setGeometry(5 + (btn_widht + 5)*3,200 + i*(btn_height + 5),btn_widht,100);
//删除那里单独添加处理,其余的统一绑定到同一个处理函数里面处理
if((cout + 2) != 2){
QObject::connect(btn[cout + 2],SIGNAL(clicked(bool)),this,SLOT(btn_handler(bool)));
}
QObject::connect(btn[cout + 0],SIGNAL(clicked(bool)),this,SLOT(btn_handler(bool)));
QObject::connect(btn[cout + 1],SIGNAL(clicked(bool)),this,SLOT(btn_handler(bool)));
QObject::connect(btn[cout + 3],SIGNAL(clicked(bool)),this,SLOT(btn_handler(bool)));
}
cout = 0;
btn[cout + 2]->setIcon(QIcon(":/del.png"));
cout = 4 * 4;
btn[cout + 3]->setStyleSheet("QPushButton{background-color:rgb(125,183,234);border:none;}QPushButton:hover{background-color:rgb(64,148,223);}QPushButton:pressed{background-color:rgb(36,130,215);}");
QObject::connect(btn[2],SIGNAL(clicked(bool)),this,SLOT(delete_btn_handle(bool)));
}
void Widget::btn_handler(bool)
{
QPushButton *btn = dynamic_cast<QPushButton *>(sender());
QString text = btn->text();
if((text >= '0' && text <= '9') || (text == ".")){
if(fist_str.isEmpty()){
if(!result.isEmpty()){
result.clear();
this->lab_last->setText("");
}
if((dis_data.contains('.'))&&(text == ".")){
return;
}
dis_data += text;
if((dis_data.size() == 2) && dis_data[0] == '0'){
if(text == '.'){
this->lab_now->setText(dis_data);
}
else{
dis_data.clear();
dis_data += text;
this->lab_now->setText(dis_data);
}
}
else{
this->lab_now->setText(dis_data);
}
}
else {
if((second_str.contains('.'))&&(text == ".")){
return;
}
second_str += text;
if((second_str.size() == 2) && second_str[0] == '0'){
if(text == '.'){
this->lab_now->setText(second_str);
}
else{
second_str.clear();
second_str += text;
this->lab_now->setText(second_str);
}
}
else{
this->lab_now->setText(second_str);
}
}
}
else{
if(text == "C"){
this->lab_now->setText("0");
this->lab_last->setText("");
fist_str.clear();
signed_str.clear();
dis_data.clear();
second_str.clear();
result.clear();
}
else if((text == "÷") || (text == "×") ||(text == "-") ||(text == "+")){
if(!second_str.isEmpty()){ //第二个参数已经有了,不支持再次修改了
return;
}
if(result.isEmpty()){
fist_str = dis_data;
}
else{
fist_str.clear();
fist_str = result;
}
signed_str = text;
this->lab_last->setText(fist_str + text);
}
else if((text == "=")){
if(fist_str.isEmpty()){
if(dis_data.isEmpty()){
this->lab_last->setText("0=");
}
else{
this->lab_last->setText(dis_data + '=');
this->lab_now->setText(dis_data);
}
}
else{
calculator();
}
}
else if(text == "+/-"){
if(fist_str.isEmpty()){
if(dis_data.contains('-')){
dis_data.remove('-');
}
else{
dis_data = '-' + dis_data;
}
this->lab_now->setText(dis_data);
}
else{
if(second_str.contains('-')){
second_str.remove('-');
}
else{
second_str = '-' + second_str;
}
this->lab_now->setText(second_str);
}
}
else if(text == "%"){
if(fist_str.isEmpty()){
dis_data.clear();
this->lab_now->setText("0");
this->lab_last->setText("0");
}
else{
double new_first = fist_str.toDouble();
double new_second = second_str.toDouble();
new_second = new_first * (new_second/100);
second_str = second_str.setNum(new_second,'f',2);//方法2
calculator();
}
}
}
}
void Widget::delete_btn_handle(bool)
{
if(fist_str.isEmpty()){
dis_data.chop(1); //丢掉最后一个字符
if(dis_data.isEmpty()){
this->lab_now->setText("0");
}
else{
this->lab_now->setText(dis_data);
}
}
else {
second_str.chop(1); //丢掉最后一个字符
if(second_str.isEmpty()){
this->lab_now->setText("0");
}
else{
this->lab_now->setText(second_str);
}
}
}
void Widget::calculator(void)
{
this->lab_last->setText(fist_str + signed_str+second_str);
double fist = fist_str.toDouble();
double sencod = second_str.toDouble();
double ret = 0;
if(signed_str == "÷"){
if( sencod <= 0.0000000001 && sencod >= -0.0000000001){
this->lab_now->setText("除数不能为0!");
this->lab_last->setText("");
fist_str.clear();
signed_str.clear();
dis_data.clear();
second_str.clear();
result.clear();
return ;
}
else{
ret = fist / sencod;
}
}
else if(signed_str == "×"){
ret = fist * sencod;
}
else if(signed_str == "-"){
ret = fist - sencod;
}
else if(signed_str == "+"){
ret = fist + sencod;
}
result = QString("%1").arg(ret);
this->lab_now->setText(result);
fist_str.clear();
signed_str.clear();
dis_data.clear();
second_str.clear();
}
main.cpp就是默认创建的时候,我没有做修改,在这里我就不贴出来了。
转载:https://blog.csdn.net/weixin_42547950/article/details/104546202
查看评论