qt如何设计一个计算器代码
时间: 2024-04-30 15:22:03 浏览: 146
设计一个计算器需要以下步骤:
1. 定义计算器的界面:设计按钮、文本框等控件
2. 实现按钮的点击事件:每个按钮代表一个数字或操作符,需要在点击时将其显示在文本框中
3. 实现计算功能:将用户输入的表达式进行计算并显示结果
以下是一个简单的Qt计算器的代码示例:
mainwindow.h:
```c++
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_btn_0_clicked();
void on_btn_1_clicked();
void on_btn_2_clicked();
void on_btn_3_clicked();
void on_btn_4_clicked();
void on_btn_5_clicked();
void on_btn_6_clicked();
void on_btn_7_clicked();
void on_btn_8_clicked();
void on_btn_9_clicked();
void on_btn_add_clicked();
void on_btn_sub_clicked();
void on_btn_mul_clicked();
void on_btn_div_clicked();
void on_btn_clear_clicked();
void on_btn_equal_clicked();
private:
Ui::MainWindow *ui;
QString expression;
};
#endif // MAINWINDOW_H
```
mainwindow.cpp:
```c++
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
expression = "";
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btn_0_clicked()
{
expression += "0";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_1_clicked()
{
expression += "1";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_2_clicked()
{
expression += "2";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_3_clicked()
{
expression += "3";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_4_clicked()
{
expression += "4";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_5_clicked()
{
expression += "5";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_6_clicked()
{
expression += "6";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_7_clicked()
{
expression += "7";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_8_clicked()
{
expression += "8";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_9_clicked()
{
expression += "9";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_add_clicked()
{
expression += "+";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_sub_clicked()
{
expression += "-";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_mul_clicked()
{
expression += "*";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_div_clicked()
{
expression += "/";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_clear_clicked()
{
expression = "";
ui->lineEdit->setText(expression);
}
void MainWindow::on_btn_equal_clicked()
{
QStringList expList = expression.split(QRegExp("\\+|-|\\*|/"));
QStringList opList;
for (int i = 0; i < expression.length(); i++) {
if (expression[i] == "+" || expression[i] == "-" || expression[i] == "*" || expression[i] == "/") {
opList.append(QString(expression[i]));
}
}
double result = expList[0].toDouble();
for (int i = 1; i < expList.size(); i++) {
if (opList[i-1] == "+") {
result += expList[i].toDouble();
} else if (opList[i-1] == "-") {
result -= expList[i].toDouble();
} else if (opList[i-1] == "*") {
result *= expList[i].toDouble();
} else if (opList[i-1] == "/") {
result /= expList[i].toDouble();
}
}
expression = QString::number(result);
ui->lineEdit->setText(expression);
}
```
在Qt Designer中设计界面,将按钮和文本框等控件拖拽到主窗口中,然后将每个按钮的clicked()信号连接到对应的槽函数上即可。在每个槽函数中,将按钮代表的数字或操作符添加到表达式中,并显示在文本框中。在“=”按钮的槽函数中,将表达式进行解析计算,并将结果显示在文本框中。
阅读全文