使用Visual Studio 2022和Qt插件开发方式,实现了一个简单的计算器应用程序的具体代码
时间: 2024-02-20 11:56:14 浏览: 145
windows下安装QT及visual studio 2017搭建开发环境
5星 · 资源好评率100%
这里提供一个基于Visual Studio 2022和Qt插件开发的简单计算器应用程序的示例代码。
MainWindow.h头文件:
```cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QLineEdit>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void onDigitClicked();
void onOperatorClicked();
void onEqualsClicked();
void onClearClicked();
private:
QPushButton* m_digits[10];
QPushButton* m_operators[4];
QPushButton* m_equals;
QPushButton* m_clear;
QLineEdit* m_display;
QString m_currentValue;
QString m_pendingValue;
QString m_pendingOperator;
};
#endif // MAINWINDOW_H
```
MainWindow.cpp源文件:
```cpp
#include "MainWindow.h"
#include <QGridLayout>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// 设置窗口标题
setWindowTitle("Calculator");
// 创建数字按钮
for (int i = 0; i < 10; ++i)
{
m_digits[i] = new QPushButton(QString::number(i), this);
connect(m_digits[i], SIGNAL(clicked()), this, SLOT(onDigitClicked()));
}
// 创建运算符按钮
m_operators[0] = new QPushButton("+", this);
m_operators[1] = new QPushButton("-", this);
m_operators[2] = new QPushButton("*", this);
m_operators[3] = new QPushButton("/", this);
for (int i = 0; i < 4; ++i)
{
connect(m_operators[i], SIGNAL(clicked()), this, SLOT(onOperatorClicked()));
}
// 创建等于号、清除按钮和显示框
m_equals = new QPushButton("=", this);
m_clear = new QPushButton("C", this);
m_display = new QLineEdit(this);
// 创建布局
QGridLayout* layout = new QGridLayout;
layout->addWidget(m_display, 0, 0, 1, 4);
layout->addWidget(m_clear, 1, 0);
layout->addWidget(m_equals, 1, 1);
layout->addWidget(m_operators[0], 1, 2);
layout->addWidget(m_operators[1], 2, 2);
layout->addWidget(m_operators[2], 3, 2);
layout->addWidget(m_operators[3], 4, 2);
for (int i = 1, row = 4, col = 0; i < 10; ++i)
{
if (col == 3)
{
++row;
col = 0;
}
else
{
++col;
}
layout->addWidget(m_digits[i], row, col);
}
layout->addWidget(m_digits[0], 5, 0, 1, 2);
// 设置窗口布局
QWidget* centralWidget = new QWidget(this);
centralWidget->setLayout(layout);
setCentralWidget(centralWidget);
// 初始化变量
m_currentValue = "";
m_pendingValue = "";
m_pendingOperator = "";
}
MainWindow::~MainWindow()
{
}
void MainWindow::onDigitClicked()
{
QPushButton* button = qobject_cast<QPushButton*>(sender());
if (button)
{
m_currentValue += button->text();
m_display->setText(m_currentValue);
}
}
void MainWindow::onOperatorClicked()
{
QPushButton* button = qobject_cast<QPushButton*>(sender());
if (button)
{
// 如果存在待处理的值和运算符,先计算结果
if (!m_pendingValue.isEmpty() && !m_pendingOperator.isEmpty())
{
double value = m_pendingValue.toDouble();
double currentValue = m_currentValue.toDouble();
if (m_pendingOperator == "+")
{
value += currentValue;
}
else if (m_pendingOperator == "-")
{
value -= currentValue;
}
else if (m_pendingOperator == "*")
{
value *= currentValue;
}
else if (m_pendingOperator == "/")
{
value /= currentValue;
}
m_pendingValue = QString::number(value);
m_display->setText(m_pendingValue);
}
else
{
m_pendingValue = m_currentValue;
}
m_currentValue = "";
m_pendingOperator = button->text();
}
}
void MainWindow::onEqualsClicked()
{
if (!m_pendingValue.isEmpty() && !m_pendingOperator.isEmpty())
{
double value = m_pendingValue.toDouble();
double currentValue = m_currentValue.toDouble();
if (m_pendingOperator == "+")
{
value += currentValue;
}
else if (m_pendingOperator == "-")
{
value -= currentValue;
}
else if (m_pendingOperator == "*")
{
value *= currentValue;
}
else if (m_pendingOperator == "/")
{
value /= currentValue;
}
m_pendingValue = "";
m_pendingOperator = "";
m_currentValue = QString::number(value);
m_display->setText(m_currentValue);
}
}
void MainWindow::onClearClicked()
{
m_currentValue = "";
m_pendingValue = "";
m_pendingOperator = "";
m_display->setText("");
}
```
这里的示例代码实现了一个简单的计算器应用程序,包括数字按钮、运算符按钮、等于号、清除按钮和显示框,同时利用了Qt的信号和槽机制,实现了计算器的基本功能。需要注意的是,在使用示例代码时,需要根据实际需要进行修改和适配。
阅读全文