用QT+vs混合编程用插模机制实现简易计算器的案例
时间: 2024-03-08 08:46:15 浏览: 108
好的,这是一个用QT+VS混合编程,使用插模机制实现的简易计算器案例:
1. 首先,在QT中创建一个新的工程(例如,命名为Calculator),选择“QT Widgets Application”。
2. 在VS中打开Calculator.pro文件,将以下代码添加到文件末尾:
```
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/ -lCalculator
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/ -lCalculatord
```
3. 在VS中新建一个工程(例如,命名为CalculatorDLL),选择“DLL”模板。
4. 将以下代码添加到CalculatorDLL.h文件中:
```
#ifndef CALCULATORDLL_H
#define CALCULATORDLL_H
#include <QtCore/qglobal.h>
#include <QtPlugin>
class CalculatorDLL
{
public:
virtual double add(double a, double b) = 0;
virtual double subtract(double a, double b) = 0;
virtual double multiply(double a, double b) = 0;
virtual double divide(double a, double b) = 0;
};
Q_DECLARE_INTERFACE(CalculatorDLL, "com.example.CalculatorDLL/1.0")
#endif // CALCULATORDLL_H
```
5. 在CalculatorDLL.cpp中,将以下代码添加到文件末尾:
```
#include "CalculatorDLL.h"
class CalculatorPlugin : public CalculatorDLL
{
public:
double add(double a, double b) override
{
return a + b;
}
double subtract(double a, double b) override
{
return a - b;
}
double multiply(double a, double b) override
{
return a * b;
}
double divide(double a, double b) override
{
return a / b;
}
};
Q_EXPORT_PLUGIN2(CalculatorPlugin, CalculatorDLL)
```
6. 在QT中打开Calculator.cpp文件,将以下代码添加到文件末尾:
```
#include "CalculatorDLL.h"
void Calculator::loadPlugins()
{
QDir pluginsDir(qApp->applicationDirPath());
pluginsDir.cd("plugins");
foreach (QString fileName, pluginsDir.entryList(QDir::Files))
{
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
QObject *plugin = pluginLoader.instance();
if (plugin)
{
CalculatorDLL *calculator = qobject_cast<CalculatorDLL *>(plugin);
if (calculator)
{
m_calculators.append(calculator);
}
}
}
}
void Calculator::unloadPlugins()
{
foreach (CalculatorDLL *calculator, m_calculators)
{
delete calculator;
}
m_calculators.clear();
}
void Calculator::on_addBtn_clicked()
{
double a = ui->num1Edit->text().toDouble();
double b = ui->num2Edit->text().toDouble();
double result = 0;
foreach (CalculatorDLL *calculator, m_calculators)
{
result += calculator->add(a, b);
}
ui->resultLabel->setText(QString::number(result));
}
void Calculator::on_subtractBtn_clicked()
{
double a = ui->num1Edit->text().toDouble();
double b = ui->num2Edit->text().toDouble();
double result = 0;
foreach (CalculatorDLL *calculator, m_calculators)
{
result += calculator->subtract(a, b);
}
ui->resultLabel->setText(QString::number(result));
}
void Calculator::on_multiplyBtn_clicked()
{
double a = ui->num1Edit->text().toDouble();
double b = ui->num2Edit->text().toDouble();
double result = 0;
foreach (CalculatorDLL *calculator, m_calculators)
{
result += calculator->multiply(a, b);
}
ui->resultLabel->setText(QString::number(result));
}
void Calculator::on_divideBtn_clicked()
{
double a = ui->num1Edit->text().toDouble();
double b = ui->num2Edit->text().toDouble();
double result = 0;
foreach (CalculatorDLL *calculator, m_calculators)
{
result += calculator->divide(a, b);
}
ui->resultLabel->setText(QString::number(result));
}
```
7. 在QT中打开Calculator.h文件,将以下代码添加到文件末尾:
```
#include <QtCore/QDir>
#include <QtCore/QPluginLoader>
#include <QtCore/QList>
#include "CalculatorDLL.h"
class Calculator : public QMainWindow
{
Q_OBJECT
public:
void loadPlugins();
void unloadPlugins();
private slots:
void on_addBtn_clicked();
void on_subtractBtn_clicked();
void on_multiplyBtn_clicked();
void on_divideBtn_clicked();
private:
Ui::CalculatorClass ui;
QList<CalculatorDLL *> m_calculators;
};
```
8. 在QT中打开Calculator.ui文件,添加4个QPushButton和2个QLineEdit,分别命名为addBtn、subtractBtn、multiplyBtn、divideBtn、num1Edit、num2Edit和resultLabel。
9. 在QT中打开Calculator.pro文件,将以下代码添加到文件末尾:
```
TARGET = Calculator
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp\
Calculator.cpp
HEADERS += Calculator.h
FORMS += Calculator.ui
CONFIG += plugin
PLUGIN_SUFFIX = .dll
PLUGIN_DESTINATION = plugins
```
10. 在VS中打开CalculatorDLL的属性页,选择“配置属性”->“常规”,将输出目录设置为Calculator工程的plugins目录。
11. 在VS中打开CalculatorDLL的属性页,选择“配置属性”->“C/C++”->“代码生成”,将运行库设置为“多线程DLL (/MD)”。
12. 编译并运行程序,即可使用简易计算器。可以在CalculatorDLL工程中添加新的插件实现不同的计算方式,并在运行时动态加载。
阅读全文