test_QPluginLoader.h
#pragma once
#include "test_macro.h"
#include <QWidget>
#define QtPluginDemo_iid "org.qt-project.Qt.PluginDemo" // 定义接口的IID
class TESTMODEL_API IUIPLUGINBASE
{
public:
//初始化 失败返回-1
// virtual int init() = 0;
//input:显示的起始位置x , y 轴坐标
virtual int show(int pos_x, int pos_y = 0) = 0;
};
Q_DECLARE_INTERFACE(IUIPLUGINBASE, QtPluginDemo_iid);
class MainDlg : public QWidget, public IUIPLUGINBASE
{
Q_OBJECT
Q_PLUGIN_METADATA(IID QtPluginDemo_iid)
Q_INTERFACES(IUIPLUGINBASE)
public:
explicit MainDlg(QWidget *parent = 0);
~MainDlg() {}
//input:显示的起始位置x , y 轴坐标
int show(int pos_x, int pos_y);
private slots:
private:
};
test_QPluginLoader.cpp
#include "stdafx.h"
#include "test_QPluginLoader.h"
#include <QPluginLoader>
MainDlg::MainDlg(QWidget *parent) :
QWidget(parent)
{
this->setWindowTitle("test_QPluginLoader");
}
int MainDlg::show(int pos_x, int pos_y)
{
move(pos_x, pos_y);
QWidget::show();
return 1;
}
void main()
{
string path = fmt::format("{0}/{1}.dll", app::app_dir_path, "TestModule");
QPluginLoader loader(path.c_str());
IUIPLUGINBASE * P_plug;
QObject* loaderplugin = loader.instance();
if (!loader.isLoaded())
qDebug() << loader.errorString();
if (loaderplugin) {
P_plug = qobject_cast<IUIPLUGINBASE * >(loaderplugin);
P_plug->show(100, 100);
}
}