使用qt制作一个计算器
时间: 2024-04-26 07:22:22 浏览: 92
【Qt 教程篇】从小白进阶成Qt大神-基础篇 登录跳转界面
好的,下面是一个简单的基于Qt的计算器示例代码。你可以根据需要自行修改。
```cpp
#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
class Calculator : public QWidget
{
Q_OBJECT
public:
Calculator(QWidget *parent = nullptr) : QWidget(parent)
{
// 设置窗口标题和大小
setWindowTitle(tr("Calculator"));
setFixedSize(250, 200);
// 创建文本框
QLineEdit *lineEdit = new QLineEdit(this);
lineEdit->setReadOnly(true);
lineEdit->setAlignment(Qt::AlignRight);
lineEdit->setText("0");
// 创建按钮
QPushButton *button0 = new QPushButton(tr("0"), this);
QPushButton *button1 = new QPushButton(tr("1"), this);
QPushButton *button2 = new QPushButton(tr("2"), this);
QPushButton *button3 = new QPushButton(tr("3"), this);
QPushButton *button4 = new QPushButton(tr("4"), this);
QPushButton *button5 = new QPushButton(tr("5"), this);
QPushButton *button6 = new QPushButton(tr("6"), this);
QPushButton *button7 = new QPushButton(tr("7"), this);
QPushButton *button8 = new QPushButton(tr("8"), this);
QPushButton *button9 = new QPushButton(tr("9"), this);
QPushButton *buttonAdd = new QPushButton(tr("+"), this);
QPushButton *buttonSubtract = new QPushButton(tr("-"), this);
QPushButton *buttonMultiply = new QPushButton(tr("*"), this);
QPushButton *buttonDivide = new QPushButton(tr("/"), this);
QPushButton *buttonClear = new QPushButton(tr("C"), this);
QPushButton *buttonEqual = new QPushButton(tr("="), this);
// 创建网格布局
QGridLayout *gridLayout = new QGridLayout(this);
gridLayout->addWidget(lineEdit, 0, 0, 1, 4);
gridLayout->addWidget(button7, 1, 0);
gridLayout->addWidget(button8, 1, 1);
gridLayout->addWidget(button9, 1, 2);
gridLayout->addWidget(buttonAdd, 1, 3);
gridLayout->addWidget(button4, 2, 0);
gridLayout->addWidget(button5, 2, 1);
gridLayout->addWidget(button6, 2, 2);
gridLayout->addWidget(buttonSubtract, 2, 3);
gridLayout->addWidget(button1, 3, 0);
gridLayout->addWidget(button2, 3, 1);
gridLayout->addWidget(button3, 3, 2);
gridLayout->addWidget(buttonMultiply, 3, 3);
gridLayout->addWidget(buttonClear, 4, 0);
gridLayout->addWidget(button0, 4, 1);
gridLayout->addWidget(buttonEqual, 4, 2);
gridLayout->addWidget(buttonDivide, 4, 3);
// 创建垂直布局
QVBoxLayout *vBoxLayout = new QVBoxLayout(this);
vBoxLayout->addLayout(gridLayout);
setLayout(vBoxLayout);
// 信号和槽连接
connect(button0, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(button1, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(button2, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(button3, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(button4, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(button5, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(button6, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(button7, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(button8, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(button9, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(buttonAdd, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(buttonSubtract, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(buttonMultiply, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(buttonDivide, SIGNAL(clicked()), this, SLOT(buttonClicked()));
connect(buttonClear, SIGNAL(clicked()), this, SLOT(clear()));
connect(buttonEqual, SIGNAL(clicked()), this, SLOT(result()));
}
public slots:
void buttonClicked()
{
QPushButton *button = qobject_cast<QPushButton *>(sender());
QString buttonValue = button->text();
QLineEdit *lineEdit = findChild<QLineEdit *>("lineEdit");
QString lineEditText = lineEdit->text();
if (lineEditText == "0")
{
lineEditText = "";
}
lineEditText += buttonValue;
lineEdit->setText(lineEditText);
}
void clear()
{
QLineEdit *lineEdit = findChild<QLineEdit *>("lineEdit");
lineEdit->setText("0");
}
void result()
{
QLineEdit *lineEdit = findChild<QLineEdit *>("lineEdit");
QString expression = lineEdit->text();
double result = 0;
int operatorIndex = -1;
if (expression.contains("+"))
{
operatorIndex = expression.indexOf("+");
result = expression.left(operatorIndex).toDouble() + expression.right(expression.length() - operatorIndex - 1).toDouble();
}
else if (expression.contains("-"))
{
operatorIndex = expression.indexOf("-");
result = expression.left(operatorIndex).toDouble() - expression.right(expression.length() - operatorIndex - 1).toDouble();
}
else if (expression.contains("*"))
{
operatorIndex = expression.indexOf("*");
result = expression.left(operatorIndex).toDouble() * expression.right(expression.length() - operatorIndex - 1).toDouble();
}
else if (expression.contains("/"))
{
operatorIndex = expression.indexOf("/");
result = expression.left(operatorIndex).toDouble() / expression.right(expression.length() - operatorIndex - 1).toDouble();
}
lineEdit->setText(QString::number(result));
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Calculator calculator;
calculator.show();
return a.exec();
}
#include "main.moc"
```
这个计算器界面比较简单,只有一个文本框和一些按钮。你可以根据需求自己修改界面和代码。
阅读全文