#include "testCommPlugin.h"
#include <QDir>
#include <QApplication>
#include <QMessageBox>
#include <QPluginLoader>
#include <QVBoxLayout>
#include <QDebug>
#include <QPushButton>
testCommPlugin::testCommPlugin(QWidget *parent)
: QMainWindow(parent)
{
_editInput1 = new QLineEdit;
_editInput2 = new QLineEdit;
_editInput1->setText(QString::number(33));
_editInput2->setText(QString::number(22));
QHBoxLayout* layoutIn = new QHBoxLayout;
layoutIn->addWidget(_editInput1);
layoutIn->addWidget(_editInput2);
_editOutput = new QLineEdit;
_comboBoxOper = new QComboBox;
QPushButton* buttonCali = new QPushButton(QStringLiteral("计算"));
connect(buttonCali, &QPushButton::clicked, this, [&]() { on_pushButton_clicked(); });
QHBoxLayout* layoutOut = new QHBoxLayout;
layoutOut->addWidget(_editOutput);
layoutOut->addWidget(_comboBoxOper);
layoutOut->addWidget(buttonCali);
QVBoxLayout* layoutMain = new QVBoxLayout;
layoutMain->addLayout(layoutIn);
layoutMain->addLayout(layoutOut);
QWidget* pCenterWidget = new QWidget;
setCentralWidget(pCenterWidget);
pCenterWidget->setLayout(layoutMain);
if (!loadPlugin())
return;
}
testCommPlugin::~testCommPlugin()
{}
bool testCommPlugin::loadPlugin()
{
QDir pluginsDir(qApp->applicationDirPath()); //pluginsDir:"../build-xxx-debug/debug"
#if defined(Q_OS_WIN)
//if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
// pluginsDir.cdUp(); //pluginsDir:"../build-xxx-debug"
//pluginsDir.cdUp(); //pluginsDir:"../"
#elif defined(Q_OS_MAC)
if (pluginsDir.dirName() == "MacOS") {
pluginsDir.cdUp();
pluginsDir.cdUp();
pluginsDir.cdUp();
}
#endif
//pluginsDir.cd("plugins");
foreach(QString fileName, pluginsDir.entryList(QDir::Files))
{
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
QObject* plugin = pluginLoader.instance();
qDebug() << __FUNCTION__ << pluginLoader.errorString();
if (!plugin)
continue;
DeclareInterface* pluginOper = qobject_cast<DeclareInterface*>(plugin);
if (!pluginOper)
continue;
_comboBoxOper->addItem(pluginOper->name());
mapPluginOper[pluginOper->name()] = pluginOper;
}
//vecPluginOper.push_back(qobject_cast<DeclareInterface*>(QPluginLoader(pluginsDir.absoluteFilePath("PluginAdd.dll")).instance()));
//vecPluginOper.push_back(qobject_cast<DeclareInterface*>(QPluginLoader(pluginsDir.absoluteFilePath("PluginAdd.dll")).instance()));
//vecPluginOper.push_back(qobject_cast<DeclareInterface*>(QPluginLoader(pluginsDir.absoluteFilePath("PluginSub.dll")).instance()));
return false;
}
void testCommPlugin::on_pushButton_clicked()
{
QString pluginName = _comboBoxOper->currentText();
DeclareInterface* pluginOper = mapPluginOper[pluginName];
int a = _editInput1->text().toInt();
int b = _editInput2->text().toInt();
int r = -1;
if (pluginOper) {
r = pluginOper->oper(a, b);
}
_editOutput->setText(QString::number(r));
DeclareInterface* pluginOper1 = mapPluginOper[pluginName];
DeclareInterface* pluginOper2 = mapPluginOper[pluginName];
int r1 = pluginOper1->oper(a, b);
int r2 = pluginOper2->oper(a, b);
return;
}
没有合适的资源?快使用搜索试试~ 我知道了~
Qt自定义插件的构建和使用
共23个文件
h:6个
cpp:4个
filters:3个
需积分: 2 1 下载量 126 浏览量
2022-11-08
16:46:25
上传
评论
收藏 14KB ZIP 举报
温馨提示
完整工程。实现了加法插件和减法插件,主程序加载不同的插件实现不同的操作
资源推荐
资源详情
资源评论
收起资源包目录
testCommPlugin.zip (23个子文件)
testCommPlugin
testCommPlugin.qrc 74B
testCommPlugin.vcxproj.user 560B
testCommPlugin.h 528B
main.cpp 199B
testCommPlugin.vcxproj 5KB
testCommPlugin.cpp 3KB
declareinterface.h 364B
testCommPlugin.vcxproj.filters 2KB
testCommPlugin.sln 2KB
PluginAdd
PluginAdd.cpp 151B
PluginAdd.vcxproj 5KB
pluginadd_global.h 241B
PluginAdd.vcxproj.filters 2KB
qtplugin.json 0B
PluginAdd.h 425B
PluginAdd.vcxproj.user 560B
PluginSub
pluginsub_global.h 241B
PluginSub.vcxproj.filters 1KB
PluginSub.vcxproj.user 560B
PluginSub.vcxproj 5KB
PluginSub.cpp 151B
qtplugin.json 0B
PluginSub.h 425B
共 23 条
- 1
资源评论
三石目
- 粉丝: 127
- 资源: 21
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功