帮忙用C++ Qt写一个简单的界面,界面是计算器,可以进行简单的加减乘除计算。
时间: 2023-12-14 10:38:55 浏览: 92
好的,下面是一个简单的计算器界面的代码,使用了Qt框架和C++语言。你可以根据自己的需求进行修改和扩展。
```cpp
// mainwindow.h
#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:
QLineEdit *m_lineEdit;
QPushButton *m_btnPlus;
QPushButton *m_btnMinus;
QPushButton *m_btnMultiply;
QPushButton *m_btnDivide;
QPushButton *m_btnEqual;
QPushButton *m_btnClear;
double m_firstNumber;
double m_secondNumber;
QString m_operation;
private slots:
void onBtnClicked();
void onClearClicked();
};
#endif // MAINWINDOW_H
```
```cpp
// mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
m_lineEdit = new QLineEdit(this);
m_lineEdit->setGeometry(10, 10, 280, 30);
m_btnPlus = new QPushButton("+", this);
m_btnPlus->setGeometry(10, 50, 60, 30);
connect(m_btnPlus, &QPushButton::clicked, this, &MainWindow::onBtnClicked);
m_btnMinus = new QPushButton("-", this);
m_btnMinus->setGeometry(80, 50, 60, 30);
connect(m_btnMinus, &QPushButton::clicked, this, &MainWindow::onBtnClicked);
m_btnMultiply = new QPushButton("*", this);
m_btnMultiply->setGeometry(150, 50, 60, 30);
connect(m_btnMultiply, &QPushButton::clicked, this, &MainWindow::onBtnClicked);
m_btnDivide = new QPushButton("/", this);
m_btnDivide->setGeometry(220, 50, 60, 30);
connect(m_btnDivide, &QPushButton::clicked, this, &MainWindow::onBtnClicked);
m_btnEqual = new QPushButton("=", this);
m_btnEqual->setGeometry(220, 90, 60, 30);
connect(m_btnEqual, &QPushButton::clicked, this, &MainWindow::onBtnClicked);
m_btnClear = new QPushButton("Clear", this);
m_btnClear->setGeometry(150, 90, 60, 30);
connect(m_btnClear, &QPushButton::clicked, this, &MainWindow::onClearClicked);
m_firstNumber = 0;
m_secondNumber = 0;
m_operation = "";
}
MainWindow::~MainWindow()
{
}
void MainWindow::onBtnClicked()
{
QPushButton *btn = qobject_cast<QPushButton*>(sender());
if (btn == m_btnPlus) {
m_firstNumber = m_lineEdit->text().toDouble();
m_operation = "+";
m_lineEdit->clear();
} else if (btn == m_btnMinus) {
m_firstNumber = m_lineEdit->text().toDouble();
m_operation = "-";
m_lineEdit->clear();
} else if (btn == m_btnMultiply) {
m_firstNumber = m_lineEdit->text().toDouble();
m_operation = "*";
m_lineEdit->clear();
} else if (btn == m_btnDivide) {
m_firstNumber = m_lineEdit->text().toDouble();
m_operation = "/";
m_lineEdit->clear();
} else if (btn == m_btnEqual) {
m_secondNumber = m_lineEdit->text().toDouble();
double result = 0;
if (m_operation == "+") {
result = m_firstNumber + m_secondNumber;
} else if (m_operation == "-") {
result = m_firstNumber - m_secondNumber;
} else if (m_operation == "*") {
result = m_firstNumber * m_secondNumber;
} else if (m_operation == "/") {
if (m_secondNumber != 0) {
result = m_firstNumber / m_secondNumber;
} else {
result = 0;
m_lineEdit->setText("Error: Division by zero!");
}
}
m_lineEdit->setText(QString::number(result));
}
}
void MainWindow::onClearClicked()
{
m_lineEdit->clear();
m_firstNumber = 0;
m_secondNumber = 0;
m_operation = "";
}
```
在这里,我们使用了Qt提供的LineEdit和PushButton控件来实现计算器的界面。LineEdit用于显示计算结果和用户输入的数字,PushButton用于响应用户的操作。在点击加减乘除按钮时,我们通过记录当前的数字和操作符来进行计算,最后将结果显示在LineEdit上。在点击清除按钮时,我们将所有记录的数字和操作符清零,并清空LineEdit。
阅读全文