引文
链接: https://pan.baidu.com/s/1lKyXTOS8UXJtFRjCASlcA 提取码: vh24
一份在Visual Studio的QT中写的简单的闹钟,Qt GUI Application项目,经过调试可能还是会有少些bug,现在将小玩意同大家分享,很期待有朋友一起讨论,文章项目资源可以自行下载,我在调试时的qDebug还留着,方便理解。
代码中主要依靠 IsOperatorClicked 和 IsStoredNumber 来判断是否进行运算,下面是一个简单的运行演示。
步骤 | 点击的按钮 |
---|---|
初始 | IsOperatorClicked = false; IsStoredNumber = false; |
1 | num4 |
displayLabel.append(button->text()); | |
2 | actionMul(+) |
storeNumber=4; IsStoredNumber= true | |
storeOperator=+; IsOperatorClicked= true | |
3 | num2 |
IsOperatorClicked = false | |
4 | actionEqual(=) |
calculate_result(); IsStoredNumber = false |
写文章才发现英语单词写错了尬尬,把Equal写成Euqal,就不改了哈,看到的知到它是错的
Calculator.ui
界面设计及控件命名
Calculator.h
#pragma once
#include <QMainWindow>
#include "ui_Calculator.h"
#include <QAbstractButton>
#include <QtWidgets/QButtonGroup>
#include <QtCore>
namespace Ui {
class Calculator;
}
class Calculator : public QMainWindow
{
Q_OBJECT
public:
Calculator(QWidget *parent = 0);
~Calculator();
private slots:
void numberGroup_clicked(QAbstractButton*); //123456789
void actionGroup_clicked(QAbstractButton*); //+-*/
void on_actionDel_clicked(); //Del
void on_actionEqual_clicked(); //=
void on_comma_clicked(); //.
void on_actionClear_clicked(); //C
void on_actionPercent_clicked(); //%
void on_actionSign_clicked(); //+/-
private:
Ui::CalculatorClass* ui;
//限制数值长度
const int DIGIT_LIMIT = 16;
//用于检查上一个点击的按钮是否为运算符
bool IsOperatorClicked;
//用于检查数字是否存储在内存中
bool IsStoredNumber;
//存储最后点击的运算符
QChar storedOperator;
//存储数字
double storedNumber;
//根据存储的号码和显示的号码计算结果
void calculate_result();
QButtonGroup* actionGroup; //运算符按钮组
QButtonGroup* numberGroup; //数字按钮组
};
Calculator.cpp
#include "Calculator.h"
Calculator::Calculator(QWidget* parent)
: QMainWindow(parent),
ui(new Ui::CalculatorClass),
storedNumber(0)
{
ui->setupUi(this);
//设置空显示面板
ui->displayLabel->clear();
//将operator和store number和comma标志设置为false
IsOperatorClicked = false;
IsStoredNumber = false;
//分配空间
numberGroup = new QButtonGroup;
actionGroup = new QButtonGroup;
//将按钮放入
numberGroup->addButton(ui->num1, 1);
numberGroup->addButton(ui->num2, 2);
numberGroup->addButton(ui->num3, 2);
numberGroup->addButton(ui->num4, 4);
numberGroup->addButton(ui->num5, 5);
numberGroup->addButton(ui->num6, 6);
numberGroup->addButton(ui->num7, 7);
numberGroup->addButton(ui->num8, 8);
numberGroup->addButton(ui->num9, 9);
numberGroup->addButton(ui->num0, 0);
actionGroup->addButton(ui->actionAdd, 1);
actionGroup->addButton(ui->actionSub, 2);
actionGroup->addButton(ui->actionMul, 3);
actionGroup->addButton(ui->actionDiv, 4);
actionGroup->addButton(ui->actionEuqal, 5);
//连接信号和槽
numberGroup->connect(numberGroup, SIGNAL(buttonClicked(QAbstractButton*)),
this, SLOT(numberGroup_clicked(QAbstractButton*)));
//连接信号和槽
actionGroup->connect(actionGroup, SIGNAL(buttonClicked(QAbstractButton*)),
this, SLOT(actionGroup_clicked(QAbstractButton*)));
//设置窗口固定宽度和高度
this->setFixedSize(QSize(412, 433));
}
Calculator::~Calculator()
{
delete ui;
delete numberGroup;
delete actionGroup;
}
//单击数字按钮时调用
void Calculator::numberGroup_clicked(QAbstractButton* button)
{
//获取QLabel字符串
QString displayLabel = ui->displayLabel->text();
qDebug() << "numberGroup_clicked: " << displayLabel;
/*检查上一个点击的按钮是否是运算符按钮。
*如果是,请清除显示并将标志设置为false。然后继续
*再添加新的数字。*/
if (IsOperatorClicked)
{
displayLabel.clear(); //清空之前的数字
qDebug() << "numberGroup_clicked: " << displayLabel;
IsOperatorClicked = false;
}
//超过数字限制,就不加数字
if (displayLabel.length() >= DIGIT_LIMIT) {
return;
}
//末尾添加点击的数字
displayLabel.append(button->text());
qDebug() << "numberGroup_clicked: " << displayLabel;
//将数字设回显示
ui->displayLabel->setText(displayLabel);
}
//+-*/
void Calculator::actionGroup_clicked(QAbstractButton* button)
{
/*如果上一个点击的按钮是运算符,则只需保存该运算符,然后退出
*如果上一个点击的按钮不是运算符的话,我们需要看看是否只需要保存
*如果内存中已经存储了一个数字,则执行计算并存储结果*/
if (IsOperatorClicked) //如果上一个是运算符
{
storedOperator = button->text().at(0); //保存操作符
qDebug() << "actionGroup_clicked: " << storedOperator;
}
else ////如果上一个不是运算符
{
if (IsStoredNumber) //如果有数字
{
calculate_result();
}
else
{
//从显示中获取字符串
QString displayLabel = ui->displayLabel->text();
qDebug() << "actionGroup_clicked: " << displayLabel;
//将字符串转换为双精度并保存
storedNumber = displayLabel.toDouble();
qDebug() << "actionGroup_clicked: " << storedNumber;
//设置标志,指示我们现在在内存中存储了一个数字
IsStoredNumber = true;
}
//设置上次单击的按钮是运算符的标志
IsOperatorClicked = true;
//将运算符存储在内存中
storedOperator = button->text().at(0);
qDebug() << "actionGroup_clicked: " << storedOperator;
}
}
//delete
void Calculator::on_actionDel_clicked()
{
//获取QLabel字符串
QString displayLabel = ui->displayLabel->text();
qDebug() << "on_actionDel_clicked: " << displayLabel;
//检查标签是否为空
if (displayLabel.length() == 0) return;
//从字符串中删除最后一个数字
displayLabel.QString::chop(1);
qDebug() << "on_actionDel_clicked: " << displayLabel;
//将数字设回显示
ui->displayLabel->setText(displayLabel);
}
//点击=
void Calculator::on_actionEqual_clicked()
{
//获取QLabel字符串
QString displayLabel = ui->displayLabel->text();
qDebug() << "on_actionEqual_clicked: " << displayLabel;
/*必须在内存中保存一个数字才能计算结果。
*此外,显示屏上应显示一个至少有一位数字的数字,并且
*单击的最后一个按钮不应是运算符*/
if (!IsStoredNumber || displayLabel.length() < 1 || IsOperatorClicked) return;
//计算结果并显示
calculate_result();
IsStoredNumber = false;
}
//点击.
void Calculator::on_comma_clicked()
{
//获取QLabel字符串
QString displayLabel = ui->displayLabel->text();
qDebug() << "on_comma_clicked: " << displayLabel;
/*检查是否超过数字长度
*检查是否已经存在另一个逗号。*/
if (displayLabel.length() >= (DIGIT_LIMIT - 1) || displayLabel.contains('.', Qt::CaseSensitive)) return;
//如果label为空,则添加零,然后附加逗号
if (displayLabel.length() == 0)
{
displayLabel = "0";
}
//附加逗号
displayLabel.append('.');
qDebug() << "on_comma_clicked: " << displayLabel;
//将数字设回显示
ui->displayLabel->setText(displayLabel);
}
//点击C
void Calculator::on_actionClear_clicked()
{
//清空
ui->displayLabel->clear();
qDebug() << "on_actionClear_clicked: " << ui->displayLabel->text();
IsOperatorClicked = false;
IsStoredNumber = false;
}
//点击%
void Calculator::on_actionPercent_clicked()
{
//获取QLabel字符串
QString displayLabel = ui->displayLabel->text();
qDebug() << "on_actionPercent_clicked: " << displayLabel;
double percentage = displayLabel.toDouble();
//乘以0.01就可以得到一个百分比
percentage *= 0.01;
//因为可能有溢出,所以精确的转换数字
displayLabel = QString::number(percentage, 'g', DIGIT_LIMIT);
ui->displayLabel->setText(displayLabel);
}
//点击+/-
void Calculator::on_actionSign_clicked()
{
QString displayLabel = ui->displayLabel->text();
qDebug() << "on_actionSign_clicked: " << displayLabel;
double percentage = displayLabel.toDouble();
//乘以-1就改变符号
percentage *= -1;
displayLabel = QString::number(percentage, 'g', DIGIT_LIMIT);
ui->displayLabel->setText(displayLabel);
}
//计算
void Calculator::calculate_result()
{
//获取QLabel字符串
QString displayLabel = ui->displayLabel->text();
qDebug() << "calculate_result: " << displayLabel;
//如果显示的数字以逗号结尾,就删除逗号。
if (displayLabel.endsWith('.', Qt::CaseSensitive)) {
displayLabel.QString::chop(1);
}
//根据运算符决定
if (storedOperator == '+') {
storedNumber += displayLabel.toDouble();
qDebug() << "calculate_result: " << "+";
}
else if (storedOperator == '-') {
storedNumber -= displayLabel.toDouble();
qDebug() << "calculate_result: " << "-";
}
else if (storedOperator == '*') {
storedNumber *= displayLabel.toDouble();
qDebug() << "calculate_result: " << "*";
}
else if (storedOperator == '/') {
storedNumber /= displayLabel.toDouble();
qDebug() << "calculate_result: " << "/";
}
//因为可能有溢出,所以精确的转换数字
//返回与数字n相等的字符串,该字符串根据指定的格式和精度进行格式化。有关详细信息,请参见参数格式。
displayLabel = QString::number(storedNumber, 'g', DIGIT_LIMIT);
qDebug() << "calculate_result: " << displayLabel;
//将计算好的数值呈现
ui->displayLabel->setText(displayLabel);
}
main.cpp
#include "Calculator.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Calculator w;
w.show();
return a.exec();
}
如有不足之处,还望指正 1。
如果对您有帮助可以点赞、收藏、关注,将会是我最大的动力 ↩︎