使用Qt实现使用键盘事件的计算器程序
时间: 2024-05-06 16:17:29 浏览: 107
以下是使用Qt实现使用键盘事件的计算器程序的示例代码:
mainwindow.h:
```c++
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void on_digitButton_clicked();
void on_operatorButton_clicked();
void on_equalButton_clicked();
void on_clearButton_clicked();
void on_backspaceButton_clicked();
protected:
void keyPressEvent(QKeyEvent *event);
};
#endif // MAINWINDOW_H
```
mainwindow.cpp:
```c++
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QKeyEvent>
double calcResult(double leftOperand, double rightOperand, char op);
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->resultLineEdit->setText("0.0");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_digitButton_clicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
QString digitValue = clickedButton->text();
QString currentValue = ui->resultLineEdit->text();
if (currentValue == "0.0") {
ui->resultLineEdit->setText(digitValue);
} else {
ui->resultLineEdit->setText(currentValue + digitValue);
}
}
void MainWindow::on_operatorButton_clicked()
{
QPushButton *clickedButton = qobject_cast<QPushButton *>(sender());
ui->operatorLineEdit->setText(clickedButton->text());
ui->leftOperandLineEdit->setText(ui->resultLineEdit->text());
ui->resultLineEdit->setText("0.0");
}
void MainWindow::on_equalButton_clicked()
{
QString leftOperandStr = ui->leftOperandLineEdit->text();
QString rightOperandStr = ui->resultLineEdit->text();
QString operatorStr = ui->operatorLineEdit->text();
double leftOperand = leftOperandStr.toDouble();
double rightOperand = rightOperandStr.toDouble();
double result = calcResult(leftOperand, rightOperand, operatorStr.toStdString()[0]);
ui->resultLineEdit->setText(QString::number(result));
ui->leftOperandLineEdit->setText(QString::number(result));
ui->operatorLineEdit->setText("");
}
void MainWindow::on_clearButton_clicked()
{
ui->leftOperandLineEdit->setText("");
ui->operatorLineEdit->setText("");
ui->resultLineEdit->setText("0.0");
}
void MainWindow::on_backspaceButton_clicked()
{
QString currentValue = ui->resultLineEdit->text();
if (currentValue.length() > 1) {
ui->resultLineEdit->setText(currentValue.left(currentValue.length() - 1));
} else {
ui->resultLineEdit->setText("0.0");
}
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_0:
ui->digitButton_0->click();
break;
case Qt::Key_1:
ui->digitButton_1->click();
break;
case Qt::Key_2:
ui->digitButton_2->click();
break;
case Qt::Key_3:
ui->digitButton_3->click();
break;
case Qt::Key_4:
ui->digitButton_4->click();
break;
case Qt::Key_5:
ui->digitButton_5->click();
break;
case Qt::Key_6:
ui->digitButton_6->click();
break;
case Qt::Key_7:
ui->digitButton_7->click();
break;
case Qt::Key_8:
ui->digitButton_8->click();
break;
case Qt::Key_9:
ui->digitButton_9->click();
break;
case Qt::Key_Plus:
ui->operatorButton_plus->click();
break;
case Qt::Key_Minus:
ui->operatorButton_minus->click();
break;
case Qt::Key_Asterisk:
ui->operatorButton_multiply->click();
break;
case Qt::Key_Slash:
ui->operatorButton_divide->click();
break;
case Qt::Key_Equal:
case Qt::Key_Return:
ui->equalButton->click();
break;
case Qt::Key_C:
ui->clearButton->click();
break;
case Qt::Key_Backspace:
ui->backspaceButton->click();
break;
default:
QMainWindow::keyPressEvent(event);
break;
}
}
double calcResult(double leftOperand, double rightOperand, char op)
{
switch (op) {
case '+':
return leftOperand + rightOperand;
case '-':
return leftOperand - rightOperand;
case '*':
return leftOperand * rightOperand;
case '/':
return leftOperand / rightOperand;
default:
return 0.0;
}
}
```
在该程序中,我们通过键盘事件来实现计算器的基本操作,包括输入数字、运算符、等号、清除和退格等。我们在主窗口类中重载了keyPressEvent()函数,根据用户按下的键来触发相应的按钮事件。同时,我们还定义了一些槽函数,用于处理按钮的点击事件。在这些函数中,我们可以通过获取对应按钮的指针来获取用户输入的值,并进行相应的计算和操作。
需要注意的是,在计算器程序中,我们需要定义一个calcResult()函数来计算用户输入的表达式的结果。在该函数中,我们通过switch语句来判断用户输入的运算符,并进行相应的计算。
阅读全文