用Qt实现一个计算器demo
【摘要】
GitHub Demo:Qt计算器的实现
思路
class ButtonNum : public QPushButton
{
Q_OBJECT
public:
ButtonNum(...
GitHub Demo:Qt计算器的实现
思路
class ButtonNum : public QPushButton
{
Q_OBJECT
public:
ButtonNum(int i);
int index;
private slots:
void clickButton();
signals:
void sendNumber(int number);
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
上面的代码说明了我们大概的想法:
1、定义一个数字按钮类:ButtonNum,它的作用就是提供按钮上的数字给处理计算的类WCalculator。
2、为了能使ButtonNum类能发出它上面的数字,我们自定义一个信号:sendNumber(int),那么WCalculator只要观察这个信号,就可以收到按键上的数字。
3、发出这个信号的时机就在按键被点击时,为此,我们自定义一个槽函数来连接到按键的点击事件,然后在槽函数中发出这个信号。
具体代码如下:
ButtonNum::ButtonNum(int i):QPushButton(QString::number(i)),index(i){
connect(this,SIGNAL(clicked(bool)),this,SLOT(clickButton()));
};//将自定义槽函数与按键的点击信号连接起来
void ButtonNum::clickButton(){
emit sendNumber(index); //发出信号
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
在WCalculator处理计算的过程,我们是这样想的:
class WCalculator : public QDialog
{
Q_OBJECT
public:
WCalculator(QWidget *parent = nullptr);
~WCalculator();
ButtonNum* button_num[10]; // 10 个数字
// 控制按键
QPushButton * button_plus;// 按键+
QPushButton * button_minus; // 按键-
QPushButton * button_multiply;// 按键*
QPushButton * button_devide;// 按键/
QPushButton * button_equal;// 按键=
QPushButton * button_clear_all;// 清除按键c
QLineEdit* lineEdit;// 显示框
float num1,num2,result; // 临时变量,在计算过程中使用
QString S; // 用于组装显示内容
char sign;// 用于识别控制按键,以此进行相应的操作
int mark;
void on_button_plus_clicked();// 控制按键对应的槽函数
// 控制按键对应的槽函数
private slots:
void on_button_minus_clicked();
void on_button_multiply_clicked();
void on_button_devide_clicked();
void on_button_equal_clicked();
void on_button_clear_all_clicked();
void receiveNumber(int i);
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
放在private slots下的槽函数与简单的放在public下的槽函数,在连接的写法上有些区别,举个例子吧:
private slots 形式的:
connect(button_minus,SIGNAL(clicked(bool)),this,SLOT(on_button_minus_clicked()));
- 1
public下的槽函数:
connect(button_minus,&QPushButton::clicked,this,&WCalculator::on_button_minus_clicked);
- 1
这个计算器的实现不是全面的,它只能支持形如:2 + 2 = 4 这种形式的一次计算,下次计算就得按清除键重置,然后依次按 数字 ->运算符->数字->等于号。这个例子旨在使用一些信号与槽函数的概念。
WCalculator的实现代码:
#include <QGridLayout>
#include <QDebug>
#include "wcalculator.h"
#include "./ui_wcalculator.h"
WCalculator::WCalculator(QWidget *parent)
: QDialog(parent)
{
num1 = 0.0;
num2 = 0.0;
result = 0.0;
S = "";
sign = -1;
mark = 1;
lineEdit = new QLineEdit("C");
QGridLayout* Grid = new QGridLayout;
Grid->addWidget(lineEdit,1,0,1,4,Qt::Alignment());
for(int i = 0;i<10;i++){
button_num[i] = new ButtonNum(i);
}
QPushButton* button[6];
button_plus = new QPushButton("+");
button_minus = new QPushButton("-");
button_multiply = new QPushButton("*");
button_devide = new QPushButton("/");
button_equal = new QPushButton("=");
button_clear_all = new QPushButton("c");
button[0] = button_plus;
button[1] = button_minus;
button[2] = button_multiply;
button[3] = button_devide;
button[4] = button_equal;
button[5] = button_clear_all;
for(int i =1;i<5;i++){
for(int j = 0; j < 4;j++){
int num_arr_index = (i-1)*4+j;
qDebug() << (i-1)*4+j << Qt::endl;
if(num_arr_index < 10){
Grid->addWidget((QPushButton*)button_num[num_arr_index],i+1,j,Qt::Alignment());
connect(button_num[num_arr_index],SIGNAL(sendNumber(int)),this,SLOT(receiveNumber(int)));
} else {
Grid->addWidget(button[num_arr_index - 10],i+1,j,Qt::Alignment());
}
}
}
this->setLayout(Grid);
connect(button_plus,&QPushButton::clicked,this,&WCalculator::on_button_plus_clicked);
connect(button_minus,SIGNAL(clicked(bool)),this,SLOT(on_button_minus_clicked()));
connect(button_multiply,SIGNAL(clicked(bool)),this,SLOT(on_button_multiply_clicked()));
connect(button_devide,SIGNAL(clicked(bool)),this,SLOT(on_button_devide_clicked()));
connect(button_equal,SIGNAL(clicked(bool)),this,SLOT(on_button_equal_clicked()));
connect(button_clear_all,SIGNAL(clicked(bool)),this,SLOT(on_button_clear_all_clicked()));
}
void WCalculator::receiveNumber(int number){
S += QString::number(number);
lineEdit->setText(S);
if(mark == 1){
num1 = num1 * 10 + number;
} else {
num2 = num2 * 10 + number;
}
}
void WCalculator::on_button_plus_clicked(){
S += "+";
sign = '+';
mark = 2;
lineEdit->setText(S);
}
void WCalculator::on_button_minus_clicked(){
S += "-";
sign = '-';
mark = 2;
lineEdit->setText(S);
}
void WCalculator::on_button_multiply_clicked(){
S += "*";
sign = '*';
mark = 2;
lineEdit->setText(S);
}
void WCalculator::on_button_devide_clicked(){
S += "/";
sign = '/';
mark = 2;
lineEdit->setText(S);
}
void WCalculator::on_button_equal_clicked(){
S += "=";
switch (sign) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
break;
}
S += QString("%1").arg(result);
lineEdit->setText(S);
}
void WCalculator::on_button_clear_all_clicked(){
num1 = 0.0;
num2 = 0.0;
result = 0.0;
sign = -1;
mark = 1;
S = "";
lineEdit->setText(S);
}
WCalculator::~WCalculator()
{
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/126684915
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)