創(chuàng)建一個帶Qml窗體的動態(tài)庫, 接口文件如下:
#include <QQmlApplicationEngine>
#include <QtCore/qglobal.h>
#if defined(QTDLL_LIBRARY)
# define QTDLLSHARED_EXPORT Q_DECL_EXPORT
#else
# define QTDLLSHARED_EXPORT Q_DECL_IMPORT
#endif
class QTDLLSHARED_EXPORT QmlPlugin
{
public:
void ShowWindow();
private:
QQmlApplicationEngine engine;
};
extern "C" QTDLLSHARED_EXPORT void ShowWindowApp();
通過調(diào)用接口來加載和顯示qml文件
void QmlPlugin::ShowWindow()
{
engine.load(QUrl(QLatin1String("qrc:/QmlPlugin.qml")));
}
void ShowWindowApp()
{
QmlPlugin *pp = new QmlPlugin;
pp->ShowWindow();
}
調(diào)用方加載和調(diào)用插件中的接口函數(shù):
注意dll的路徑
typedef void(*FUN1)();
QLibrary lib(QCoreApplication::applicationDirPath() + "/plugins/QmlPlugin/QmlPlugin.dll");
if (lib.load())
{
FUN1 pShow = (FUN1)lib.resolve("ShowWindowApp");
if (pShow)
{
pShow();
}
else
{
qDebug() << "fun error";
}
}
else
{
qDebug() << "load error";
}
show.gif
需要完整代碼請訪問QtQuickExamples