qt实现计算器
时间: 2023-07-03 19:11:59 浏览: 119
要实现一个基本的计算器,你可以使用Qt的QLineEdit和QPushButton类来创建用户界面。下面是一个简单的实现方法:
1. 创建一个Qt项目并打开mainwindow.ui文件。
2. 拖动QLineEdit和QPushButton控件到窗口中,并按照你想要的方式布局。
3. 给每个QPushButton控件添加一个槽函数,当用户单击按钮时会触发该函数。
4. 在每个槽函数中,你需要根据用户单击的按钮来执行相应的操作。例如,如果用户单击了数字按钮,你需要将该数字添加到QLineEdit控件中;如果用户单击了运算符按钮,你需要执行相应的计算操作并将结果显示在QLineEdit控件中。
下面是一个简单的示例代码,可以帮助你开始:
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 为每个按钮添加槽函数
connect(ui->pushButton_0, SIGNAL(clicked()), this, SLOT(digit_pressed()));
connect(ui->pushButton_1, SIGNAL(clicked()), this, SLOT(digit_pressed()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(digit_pressed()));
connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(digit_pressed()));
connect(ui->pushButton_4, SIGNAL(clicked()), this, SLOT(digit_pressed()));
connect(ui->pushButton_5, SIGNAL(clicked()), this, SLOT(digit_pressed()));
connect(ui->pushButton_6, SIGNAL(clicked()), this, SLOT(digit_pressed()));
connect(ui->pushButton_7, SIGNAL(clicked()), this, SLOT(digit_pressed()));
connect(ui->pushButton_8, SIGNAL(clicked()), this, SLOT(digit_pressed()));
connect(ui->pushButton_9, SIGNAL(clicked()), this, SLOT(digit_pressed()));
connect(ui->pushButton_add, SIGNAL(clicked()), this, SLOT(operator_pressed()));
connect(ui->pushButton_subtract, SIGNAL(clicked()), this, SLOT(operator_pressed()));
connect(ui->pushButton_multiply, SIGNAL(clicked()), this, SLOT(operator_pressed()));
connect(ui->pushButton_divide, SIGNAL(clicked()), this, SLOT(operator_pressed()));
connect(ui->pushButton_equal, SIGNAL(clicked()), this, SLOT(equal_pressed()));
connect(ui->pushButton_clear, SIGNAL(clicked()), this, SLOT(clear_pressed()));
// 初始化变量
m_value = 0.0;
m_operator = "";
m_waiting_for_second_operand = false;
}
MainWindow::~MainWindow()
{
delete ui;
}
// 数字按钮槽函数
void MainWindow::digit_pressed()
{
QPushButton* button = (QPushButton*)sender();
QString digit = button->text();
QString display_text = ui->lineEdit->text();
if (m_waiting_for_second_operand)
{
ui->lineEdit->setText(digit);
m_waiting_for_second_operand = false;
}
else
{
ui->lineEdit->setText(display_text + digit);
}
}
// 运算符按钮槽函数
void MainWindow::operator_pressed()
{
QPushButton* button = (QPushButton*)sender();
QString op = button->text();
double operand = ui->lineEdit->text().toDouble();
if (m_operator == "")
{
m_value = operand;
}
else if (m_operator == "+")
{
m_value += operand;
}
else if (m_operator == "-")
{
m_value -= operand;
}
else if (m_operator == "*")
{
m_value *= operand;
}
else if (m_operator == "/")
{
m_value /= operand;
}
ui->lineEdit->setText(QString::number(m_value));
m_waiting_for_second_operand = true;
m_operator = op;
}
// 等于按钮槽函数
void MainWindow::equal_pressed()
{
double operand = ui->lineEdit->text().toDouble();
if (m_operator == "+")
{
m_value += operand;
}
else if (m_operator == "-")
{
m_value -= operand;
}
else if (m_operator == "*")
{
m_value *= operand;
}
else if (m_operator == "/")
{
m_value /= operand;
}
ui->lineEdit->setText(QString::number(m_value));
m_operator = "";
m_waiting_for_second_operand = true;
}
// 清除按钮槽函数
void MainWindow::clear_pressed()
{
ui->lineEdit->setText("");
m_value = 0.0;
m_operator = "";
m_waiting_for_second_operand = false;
}
```
这只是一个简单的实现,如果你想进一步改进你的计算器,你可以添加更多功能,例如括号、求平方根等。
阅读全文