如何用qt的mainwindiw编写一个计算器代码
时间: 2024-05-12 09:15:30 浏览: 94
利用QT界面制作一个计算器
以下是一个简单的计算器代码示例,使用 Qt 的 QMainWindow 类:
```cpp
#include <QMainWindow>
#include <QGridLayout>
#include <QLineEdit>
#include <QPushButton>
class Calculator : public QMainWindow {
Q_OBJECT
public:
Calculator(QWidget *parent = nullptr) : QMainWindow(parent) {
QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
QGridLayout *gridLayout = new QGridLayout(centralWidget);
QLineEdit *lineEdit = new QLineEdit(centralWidget);
gridLayout->addWidget(lineEdit, 0, 0, 1, 4);
QPushButton *button0 = new QPushButton("0", centralWidget);
QPushButton *button1 = new QPushButton("1", centralWidget);
QPushButton *button2 = new QPushButton("2", centralWidget);
QPushButton *button3 = new QPushButton("3", centralWidget);
QPushButton *button4 = new QPushButton("4", centralWidget);
QPushButton *button5 = new QPushButton("5", centralWidget);
QPushButton *button6 = new QPushButton("6", centralWidget);
QPushButton *button7 = new QPushButton("7", centralWidget);
QPushButton *button8 = new QPushButton("8", centralWidget);
QPushButton *button9 = new QPushButton("9", centralWidget);
QPushButton *buttonPlus = new QPushButton("+", centralWidget);
QPushButton *buttonMinus = new QPushButton("-", centralWidget);
QPushButton *buttonMultiply = new QPushButton("*", centralWidget);
QPushButton *buttonDivide = new QPushButton("/", centralWidget);
QPushButton *buttonEquals = new QPushButton("=", centralWidget);
gridLayout->addWidget(button7, 1, 0);
gridLayout->addWidget(button8, 1, 1);
gridLayout->addWidget(button9, 1, 2);
gridLayout->addWidget(buttonPlus, 1, 3);
gridLayout->addWidget(button4, 2, 0);
gridLayout->addWidget(button5, 2, 1);
gridLayout->addWidget(button6, 2, 2);
gridLayout->addWidget(buttonMinus, 2, 3);
gridLayout->addWidget(button1, 3, 0);
gridLayout->addWidget(button2, 3, 1);
gridLayout->addWidget(button3, 3, 2);
gridLayout->addWidget(buttonMultiply, 3, 3);
gridLayout->addWidget(button0, 4, 0, 1, 2);
gridLayout->addWidget(buttonDivide, 4, 2);
gridLayout->addWidget(buttonEquals, 4, 3);
connect(button0, &QPushButton::clicked, this, [=] { addDigitToLineEdit("0"); });
connect(button1, &QPushButton::clicked, this, [=] { addDigitToLineEdit("1"); });
connect(button2, &QPushButton::clicked, this, [=] { addDigitToLineEdit("2"); });
connect(button3, &QPushButton::clicked, this, [=] { addDigitToLineEdit("3"); });
connect(button4, &QPushButton::clicked, this, [=] { addDigitToLineEdit("4"); });
connect(button5, &QPushButton::clicked, this, [=] { addDigitToLineEdit("5"); });
connect(button6, &QPushButton::clicked, this, [=] { addDigitToLineEdit("6"); });
connect(button7, &QPushButton::clicked, this, [=] { addDigitToLineEdit("7"); });
connect(button8, &QPushButton::clicked, this, [=] { addDigitToLineEdit("8"); });
connect(button9, &QPushButton::clicked, this, [=] { addDigitToLineEdit("9"); });
connect(buttonPlus, &QPushButton::clicked, this, [=] { addOperatorToLineEdit("+"); });
connect(buttonMinus, &QPushButton::clicked, this, [=] { addOperatorToLineEdit("-"); });
connect(buttonMultiply, &QPushButton::clicked, this, [=] { addOperatorToLineEdit("*"); });
connect(buttonDivide, &QPushButton::clicked, this, [=] { addOperatorToLineEdit("/"); });
connect(buttonEquals, &QPushButton::clicked, this, [=] { evaluateExpression(lineEdit->text()); });
}
private:
void addDigitToLineEdit(const QString &digit) {
QLineEdit *lineEdit = centralWidget()->findChild<QLineEdit *>();
lineEdit->insert(digit);
}
void addOperatorToLineEdit(const QString &op) {
QLineEdit *lineEdit = centralWidget()->findChild<QLineEdit *>();
lineEdit->insert(op);
}
void evaluateExpression(const QString &expression) {
QLineEdit *lineEdit = centralWidget()->findChild<QLineEdit *>();
lineEdit->setText(QString::number(eval(expression)));
}
int eval(const QString &expression) {
// TODO: Implement expression evaluation.
return 0;
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Calculator calculator;
calculator.show();
return app.exec();
}
#include "main.moc"
```
该代码创建了一个具有数字按钮、运算符按钮和等号按钮的简单计算器界面。当用户单击数字或运算符按钮时,该按钮的文本将插入到 QLineEdit 中。当用户单击等号按钮时,该表达式将计算并在 QLineEdit 中显示结果。
请注意,该代码尚未实现表达式求值的算法。你需要编写一个 eval() 函数来实现它,这超出了本答案的范围。
阅读全文