用QT 写一个计算器代码
时间: 2023-08-14 14:03:40 浏览: 148
好的,我明白了。以下是一个简单的用QT编写的计算器代码:
```c++
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *window = new QWidget();
QGridLayout *layout = new QGridLayout(window);
QLineEdit *display = new QLineEdit();
display->setReadOnly(true);
display->setAlignment(Qt::AlignRight);
layout->addWidget(display, 0, 0, 1, 4);
QPushButton *button0 = new QPushButton("0");
layout->addWidget(button0, 4, 0);
QPushButton *button1 = new QPushButton("1");
layout->addWidget(button1, 3, 0);
QPushButton *button2 = new QPushButton("2");
layout->addWidget(button2, 3, 1);
QPushButton *button3 = new QPushButton("3");
layout->addWidget(button3, 3, 2);
QPushButton *button4 = new QPushButton("4");
layout->addWidget(button4, 2, 0);
QPushButton *button5 = new QPushButton("5");
layout->addWidget(button5, 2, 1);
QPushButton *button6 = new QPushButton("6");
layout->addWidget(button6, 2, 2);
QPushButton *button7 = new QPushButton("7");
layout->addWidget(button7, 1, 0);
QPushButton *button8 = new QPushButton("8");
layout->addWidget(button8, 1, 1);
QPushButton *button9 = new QPushButton("9");
layout->addWidget(button9, 1, 2);
QPushButton *buttonPlus = new QPushButton("+");
layout->addWidget(buttonPlus, 1, 3);
QPushButton *buttonMinus = new QPushButton("-");
layout->addWidget(buttonMinus, 2, 3);
QPushButton *buttonMultiply = new QPushButton("*");
layout->addWidget(buttonMultiply, 3, 3);
QPushButton *buttonDivide = new QPushButton("/");
layout->addWidget(buttonDivide, 4, 3);
QPushButton *buttonClear = new QPushButton("Clear");
layout->addWidget(buttonClear, 4, 1);
QPushButton *buttonEquals = new QPushButton("=");
layout->addWidget(buttonEquals, 4, 2);
QSignalMapper *signalMapper = new QSignalMapper(window);
signalMapper->setMapping(button0, QString("0"));
signalMapper->setMapping(button1, QString("1"));
signalMapper->setMapping(button2, QString("2"));
signalMapper->setMapping(button3, QString("3"));
signalMapper->setMapping(button4, QString("4"));
signalMapper->setMapping(button5, QString("5"));
signalMapper->setMapping(button6, QString("6"));
signalMapper->setMapping(button7, QString("7"));
signalMapper->setMapping(button8, QString("8"));
signalMapper->setMapping(button9, QString("9"));
signalMapper->setMapping(buttonPlus, QString("+"));
signalMapper->setMapping(buttonMinus, QString("-"));
signalMapper->setMapping(buttonMultiply, QString("*"));
signalMapper->setMapping(buttonDivide, QString("/"));
QObject::connect(button0, SIGNAL(clicked()), signalMapper, SLOT(map()));
QObject::connect(button1, SIGNAL(clicked()), signalMapper, SLOT(map()));
QObject::connect(button2, SIGNAL(clicked()), signalMapper, SLOT(map()));
QObject::connect(button3, SIGNAL(clicked()), signalMapper, SLOT(map()));
QObject::connect(button4, SIGNAL(clicked()), signalMapper, SLOT(map()));
QObject::connect(button5, SIGNAL(clicked()), signalMapper, SLOT(map()));
QObject::connect(button6, SIGNAL(clicked()), signalMapper, SLOT(map()));
QObject::connect(button7, SIGNAL(clicked()), signalMapper, SLOT(map()));
QObject::connect(button8, SIGNAL(clicked()), signalMapper, SLOT(map()));
QObject::connect(button9, SIGNAL(clicked()), signalMapper, SLOT(map()));
QObject::connect(buttonPlus, SIGNAL(clicked()), signalMapper, SLOT(map()));
QObject::connect(buttonMinus, SIGNAL(clicked()), signalMapper, SLOT(map()));
QObject::connect(buttonMultiply, SIGNAL(clicked()), signalMapper, SLOT(map()));
QObject::connect(buttonDivide, SIGNAL(clicked()), signalMapper, SLOT(map()));
QObject::connect(signalMapper, SIGNAL(mapped(QString)), display, SLOT(insert(QString)));
QObject::connect(buttonClear, SIGNAL(clicked()), display, SLOT(clear()));
QObject::connect(buttonEquals, SIGNAL(clicked()), [=](){
QString expression = display->text().replace(" ", "");
QRegularExpression regex("\\d+([*/+-]\\d+)*");
if (regex.match(expression).hasMatch()) {
QScriptEngine engine;
QVariant result = engine.evaluate(expression);
display->setText(QString::number(result.toDouble()));
} else {
QMessageBox messageBox;
messageBox.setText("Invalid expression");
messageBox.exec();
}
});
window->setWindowTitle("Calculator");
window->show();
return app.exec();
}
```
此代码会创建一个带有按钮的窗口,用户可以单击这些按钮来构建一个算术表达式,然后单击 "Equals" 按钮来计算表达式并显示结果。代码使用 QRegularExpression 来验证表达式是否有效,并使用 QScriptEngine 来计算表达式的结果。
希望能帮助到您!
阅读全文