qt实现计算器
时间: 2023-07-03 14:23:52 浏览: 115
要实现一个计算器,可以使用Qt Creator创建一个新的Qt Widgets应用程序。然后,在Qt Designer中创建一个用户界面,包括数字和操作符按钮以及一个显示结果的文本框。
接下来,需要编写C++代码来实现计算器的逻辑。可以使用Qt的信号槽机制来连接按钮的点击事件和计算器逻辑的函数。例如,当数字按钮被点击时,将数字添加到正在处理的数字中。当操作符按钮被点击时,将正在处理的数字和之前的数字一起计算,并更新结果文本框。
以下是一个简单的示例代码,仅实现了加法和减法操作:
```
#include <QtCore>
#include <QtGui>
class Calculator : public QWidget
{
Q_OBJECT
public:
Calculator(QWidget *parent = 0);
private slots:
void digitClicked();
void operatorClicked();
private:
QPushButton *createButton(const QString &text, const char *member);
void calculate();
double sum;
double currentNumber;
QString currentOperator;
QLineEdit *display;
};
Calculator::Calculator(QWidget *parent)
: QWidget(parent), sum(0), currentNumber(0)
{
display = new QLineEdit("0");
display->setReadOnly(true);
display->setAlignment(Qt::AlignRight);
display->setMaxLength(15);
QGridLayout *layout = new QGridLayout;
layout->addWidget(display, 0, 0, 1, 4);
layout->addWidget(createButton("7", SLOT(digitClicked())), 1, 0);
layout->addWidget(createButton("8", SLOT(digitClicked())), 1, 1);
layout->addWidget(createButton("9", SLOT(digitClicked())), 1, 2);
layout->addWidget(createButton("+", SLOT(operatorClicked())), 1, 3);
layout->addWidget(createButton("4", SLOT(digitClicked())), 2, 0);
layout->addWidget(createButton("5", SLOT(digitClicked())), 2, 1);
layout->addWidget(createButton("6", SLOT(digitClicked())), 2, 2);
layout->addWidget(createButton("-", SLOT(operatorClicked())), 2, 3);
layout->addWidget(createButton("1", SLOT(digitClicked())), 3, 0);
layout->addWidget(createButton("2", SLOT(digitClicked())), 3, 1);
layout->addWidget(createButton("3", SLOT(digitClicked())), 3, 2);
layout->addWidget(createButton("=", SLOT(operatorClicked())), 3, 3);
layout->addWidget(createButton("0", SLOT(digitClicked())), 4, 0, 1, 2);
layout->addWidget(createButton(".", SLOT(digitClicked())), 4, 2);
layout->addWidget(createButton("C", SLOT(operatorClicked())), 4, 3);
setLayout(layout);
}
QPushButton *Calculator::createButton(const QString &text, const char *member)
{
QPushButton *button = new QPushButton(text);
connect(button, SIGNAL(clicked()), this, member);
return button;
}
void Calculator::digitClicked()
{
QPushButton *button = qobject_cast<QPushButton*>(sender());
Q_ASSERT(button != 0);
int digitValue = button->text().toInt();
currentNumber = currentNumber * 10 + digitValue;
display->setText(QString::number(currentNumber));
}
void Calculator::operatorClicked()
{
QPushButton *button = qobject_cast<QPushButton*>(sender());
Q_ASSERT(button != 0);
QString clickedOperator = button->text();
if (clickedOperator == "C") {
sum = 0;
currentNumber = 0;
currentOperator = "";
display->setText("0");
return;
}
if (!currentOperator.isEmpty()) {
calculate();
}
currentOperator = clickedOperator;
sum = currentNumber;
currentNumber = 0;
}
void Calculator::calculate()
{
if (currentOperator == "+") {
sum += currentNumber;
} else if (currentOperator == "-") {
sum -= currentNumber;
}
currentNumber = 0;
display->setText(QString::number(sum));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Calculator calc;
calc.setWindowTitle("Calculator");
calc.show();
return app.exec();
}
```
这是一个非常简单的计算器,可以通过添加更多的操作符和逻辑来扩展它。
阅读全文