用qt如何设计计算器代码
时间: 2024-05-01 15:23:11 浏览: 152
QT计算器(内含完整代码)
设计计算器代码可以分为以下几个步骤:
1. 设计UI界面:可以使用Qt Designer来设计计算器的UI界面。在UI界面中添加按钮、文本框等控件,并设置好它们的属性和布局。
2. 连接信号槽:在UI界面中的按钮被点击时,需要触发相应的计算操作。因此需要将按钮的clicked信号与相应的槽函数连接起来。
3. 编写计算函数:根据计算器的功能,编写相应的计算函数。例如,加、减、乘、除等操作。
4. 实现计算逻辑:在槽函数中调用计算函数,获取用户输入的数字和操作符,并计算得出结果。
5. 在UI界面中显示结果:将计算结果显示在UI界面中的文本框中。
下面是一个简单的计算器示例代码:
```cpp
#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLineEdit>
class Calculator : public QWidget
{
public:
Calculator(QWidget *parent = nullptr);
private slots:
void onButtonClicked();
private:
QLineEdit *m_display;
QPushButton *m_btn0;
QPushButton *m_btn1;
QPushButton *m_btn2;
QPushButton *m_btn3;
QPushButton *m_btn4;
QPushButton *m_btn5;
QPushButton *m_btn6;
QPushButton *m_btn7;
QPushButton *m_btn8;
QPushButton *m_btn9;
QPushButton *m_btnAdd;
QPushButton *m_btnSub;
QPushButton *m_btnMul;
QPushButton *m_btnDiv;
QPushButton *m_btnEqual;
};
Calculator::Calculator(QWidget *parent)
: QWidget(parent),
m_display(new QLineEdit),
m_btn0(new QPushButton("0")),
m_btn1(new QPushButton("1")),
m_btn2(new QPushButton("2")),
m_btn3(new QPushButton("3")),
m_btn4(new QPushButton("4")),
m_btn5(new QPushButton("5")),
m_btn6(new QPushButton("6")),
m_btn7(new QPushButton("7")),
m_btn8(new QPushButton("8")),
m_btn9(new QPushButton("9")),
m_btnAdd(new QPushButton("+")),
m_btnSub(new QPushButton("-")),
m_btnMul(new QPushButton("*")),
m_btnDiv(new QPushButton("/")),
m_btnEqual(new QPushButton("="))
{
m_display->setReadOnly(true);
QHBoxLayout *hLayout1 = new QHBoxLayout;
hLayout1->addWidget(m_btn0);
hLayout1->addWidget(m_btn1);
hLayout1->addWidget(m_btn2);
hLayout1->addWidget(m_btn3);
QHBoxLayout *hLayout2 = new QHBoxLayout;
hLayout2->addWidget(m_btn4);
hLayout2->addWidget(m_btn5);
hLayout2->addWidget(m_btn6);
hLayout2->addWidget(m_btn7);
QHBoxLayout *hLayout3 = new QHBoxLayout;
hLayout3->addWidget(m_btn8);
hLayout3->addWidget(m_btn9);
hLayout3->addWidget(m_btnAdd);
hLayout3->addWidget(m_btnSub);
QHBoxLayout *hLayout4 = new QHBoxLayout;
hLayout4->addWidget(m_btnMul);
hLayout4->addWidget(m_btnDiv);
hLayout4->addWidget(m_btnEqual);
QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->addWidget(m_display);
vLayout->addLayout(hLayout1);
vLayout->addLayout(hLayout2);
vLayout->addLayout(hLayout3);
vLayout->addLayout(hLayout4);
setLayout(vLayout);
connect(m_btn0, &QPushButton::clicked, this, &Calculator::onButtonClicked);
connect(m_btn1, &QPushButton::clicked, this, &Calculator::onButtonClicked);
connect(m_btn2, &QPushButton::clicked, this, &Calculator::onButtonClicked);
connect(m_btn3, &QPushButton::clicked, this, &Calculator::onButtonClicked);
connect(m_btn4, &QPushButton::clicked, this, &Calculator::onButtonClicked);
connect(m_btn5, &QPushButton::clicked, this, &Calculator::onButtonClicked);
connect(m_btn6, &QPushButton::clicked, this, &Calculator::onButtonClicked);
connect(m_btn7, &QPushButton::clicked, this, &Calculator::onButtonClicked);
connect(m_btn8, &QPushButton::clicked, this, &Calculator::onButtonClicked);
connect(m_btn9, &QPushButton::clicked, this, &Calculator::onButtonClicked);
connect(m_btnAdd, &QPushButton::clicked, this, &Calculator::onButtonClicked);
connect(m_btnSub, &QPushButton::clicked, this, &Calculator::onButtonClicked);
connect(m_btnMul, &QPushButton::clicked, this, &Calculator::onButtonClicked);
connect(m_btnDiv, &QPushButton::clicked, this, &Calculator::onButtonClicked);
connect(m_btnEqual, &QPushButton::clicked, this, &Calculator::onButtonClicked);
}
void Calculator::onButtonClicked()
{
QPushButton *btn = qobject_cast<QPushButton *>(sender());
if (!btn)
return;
QString text = btn->text();
QString displayText = m_display->text();
if (text == "+" || text == "-" || text == "*" || text == "/")
{
// 计算当前表达式的值
double leftOperand = displayText.toDouble();
double rightOperand = m_display->property("rightOperand").toDouble();
QString lastOperator = m_display->property("lastOperator").toString();
double result = 0.0;
if (lastOperator == "+")
result = leftOperand + rightOperand;
else if (lastOperator == "-")
result = leftOperand - rightOperand;
else if (lastOperator == "*")
result = leftOperand * rightOperand;
else if (lastOperator == "/")
result = leftOperand / rightOperand;
m_display->setText(QString::number(result));
m_display->setProperty("lastOperator", text);
m_display->setProperty("rightOperand", displayText);
}
else if (text == "=")
{
// 计算最终结果
double leftOperand = displayText.toDouble();
double rightOperand = m_display->property("rightOperand").toDouble();
QString lastOperator = m_display->property("lastOperator").toString();
double result = 0.0;
if (lastOperator == "+")
result = leftOperand + rightOperand;
else if (lastOperator == "-")
result = leftOperand - rightOperand;
else if (lastOperator == "*")
result = leftOperand * rightOperand;
else if (lastOperator == "/")
result = leftOperand / rightOperand;
m_display->setText(QString::number(result));
}
else
{
// 更新显示内容
displayText += text;
m_display->setText(displayText);
}
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Calculator calculator;
calculator.show();
return app.exec();
}
```
这个示例代码中,我们使用QLineEdit来显示计算器的输入和输出,使用QPushButton来实现数字和操作符的按钮。在槽函数onButtonClicked()中,我们根据用户的输入进行相应的计算并在QLineEdit中显示结果。
阅读全文