帮我写一个qt基于widget的四则运算的计算器
时间: 2023-06-27 18:01:25 浏览: 95
好的,下面是一个简单的四则运算的计算器示例:
首先,我们需要创建一个Qt Widget项目,并在窗口中添加一个QLineEdit和多个QPushButton。QLineEdit用于显示计算结果,QPushButton用于输入数字和运算符。
mainwindow.h
```cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLineEdit>
#include <QPushButton>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_numberButton_clicked();
void on_operatorButton_clicked();
void on_calculateButton_clicked();
void on_clearButton_clicked();
private:
QLineEdit *m_display;
QPushButton *m_numberButtons[10];
QPushButton *m_operatorButtons[4];
QPushButton *m_calculateButton;
QPushButton *m_clearButton;
QString m_input;
QString m_operator;
double m_result;
void createWidgets();
void createLayout();
};
#endif // MAINWINDOW_H
```
mainwindow.cpp
```cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
createWidgets();
createLayout();
}
MainWindow::~MainWindow()
{
}
void MainWindow::createWidgets()
{
m_display = new QLineEdit(this);
m_display->setReadOnly(true);
for (int i = 0; i < 10; i++) {
m_numberButtons[i] = new QPushButton(QString::number(i), this);
connect(m_numberButtons[i], SIGNAL(clicked()), this, SLOT(on_numberButton_clicked()));
}
m_operatorButtons[0] = new QPushButton("+", this);
m_operatorButtons[1] = new QPushButton("-", this);
m_operatorButtons[2] = new QPushButton("*", this);
m_operatorButtons[3] = new QPushButton("/", this);
for (int i = 0; i < 4; i++) {
connect(m_operatorButtons[i], SIGNAL(clicked()), this, SLOT(on_operatorButton_clicked()));
}
m_calculateButton = new QPushButton("=", this);
connect(m_calculateButton, SIGNAL(clicked()), this, SLOT(on_calculateButton_clicked()));
m_clearButton = new QPushButton("Clear", this);
connect(m_clearButton, SIGNAL(clicked()), this, SLOT(on_clearButton_clicked()));
}
void MainWindow::createLayout()
{
QGridLayout *layout = new QGridLayout;
layout->addWidget(m_display, 0, 0, 1, 4);
for (int i = 1; i < 10; i++) {
int row = (9 - i) / 3 + 1;
int col = (i - 1) % 3;
layout->addWidget(m_numberButtons[i], row, col);
}
layout->addWidget(m_numberButtons[0], 4, 0);
layout->addWidget(m_operatorButtons[0], 1, 3);
layout->addWidget(m_operatorButtons[1], 2, 3);
layout->addWidget(m_operatorButtons[2], 3, 3);
layout->addWidget(m_operatorButtons[3], 4, 3);
layout->addWidget(m_calculateButton, 4, 1, 1, 2);
layout->addWidget(m_clearButton, 5, 0, 1, 4);
QWidget *centralWidget = new QWidget;
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
}
void MainWindow::on_numberButton_clicked()
{
QPushButton *button = qobject_cast<QPushButton *>(sender());
m_input += button->text();
m_display->setText(m_input);
}
void MainWindow::on_operatorButton_clicked()
{
QPushButton *button = qobject_cast<QPushButton *>(sender());
m_operator = button->text();
m_result = m_input.toDouble();
m_input.clear();
}
void MainWindow::on_calculateButton_clicked()
{
if (m_operator == "+") {
m_result += m_input.toDouble();
} else if (m_operator == "-") {
m_result -= m_input.toDouble();
} else if (m_operator == "*") {
m_result *= m_input.toDouble();
} else if (m_operator == "/") {
m_result /= m_input.toDouble();
}
m_input = QString::number(m_result);
m_display->setText(m_input);
}
void MainWindow::on_clearButton_clicked()
{
m_input.clear();
m_operator.clear();
m_result = 0;
m_display->clear();
}
```
在这个示例中,我们使用QGridLayout将QLineEdit和QPushButton放置在窗口中。我们使用数组存储数字按钮和运算符按钮,并使用for循环将它们添加到布局中。
我们还实现了四个槽函数,一个用于处理数字按钮的点击事件,一个用于处理运算符按钮的点击事件,一个用于处理“=”按钮的点击事件,另一个用于处理“Clear”按钮的点击事件。当用户点击数字按钮时,我们将数字添加到输入字符串中,并将其显示在QLineEdit中。当用户点击运算符按钮时,我们将输入字符串转换为double类型,并将其存储在结果变量中,然后清空输入字符串并存储运算符。当用户点击“=”按钮时,我们执行所选运算符的计算,并在QLineEdit中显示结果。最后,当用户点击“Clear”按钮时,我们清空输入字符串,运算符和结果,并清空QLineEdit。
这是一个简单的四则运算的计算器示例,您可以根据需要进行修改和扩展。
阅读全文