演示效果:
生成生态库配置
#使用qt库列表
QT += qml quick
#又c++标准
CONFIG += c++11
#生成动态链接库(dll)
TEMPLATE = lib
#定义宏
DEFINES += QTDLL_LIBRARY
//dll输出目录
DESTDIR = $$PWD/../bin/lib/
//临时文件输出目录
OBJECTS_DIR=temp/obj
UI_DIR=temp/ui
RCC_DIR=temp/rcc
MOC_DIR=temp/moc
# 源文件
SOURCES += \
QmlPlugin.cpp
#头文件
HEADERS += \
QmlPlugin.h
#资源文件
RESOURCES += \
QmlPlugin.qrc
导出dll窗口QML源码
import QtQml 2.12
import QtQuick 2.12
import QtQuick.Controls 2.12
ApplicationWindow {
id:idExportPluginWindow
visible: true
width: 800
height: 640
title: qsTr("导出的dll插件窗口")
Button{
id:idExportBtnContrl
text: qsTr("dll插件窗口中的按钮控件")
height:100
width:parent.width
font.pointSize: 38
anchors.centerIn: parent
MouseArea{
id:mouseArea
anchors.fill: parent
onPressed: {
idExportPluginWindow.title = "点击了插件窗口的按钮";
}
}
}
}
成功输出dll库
导出库相关定义
1.定义导出宏
#if defined(QTDLL_LIBRARY)
# define QTDLLSHARED_EXPORT Q_DECL_EXPORT
#else
# define QTDLLSHARED_EXPORT Q_DECL_IMPORT
#endif
2.导出类
class QTDLLSHARED_EXPORT QmlPlugin
3.导出C风格函数
extern "C" QTDLLSHARED_EXPORT void ShowWindowApp(); //定义导出函数
4.导出函数实现
void ShowWindowApp()
{
QmlPlugin *pPlugin = new QmlPlugin;
pPlugin->ShowWindow();
}
导出类C++部分完整源码
QmlPlugin.h
#pragma once
#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;//qml引擎对象
};
extern "C" QTDLLSHARED_EXPORT void ShowWindowApp(); //定义导出函数
QmlPlugin.cpp
#include "QmlPlugin.h"
//类成员函数实现
void QmlPlugin::ShowWindow()
{
engine.load(QUrl(QLatin1String("qrc:/QmlPlugin.qml")));//加载qml资源
}
//导出函数实现
void ShowWindowApp()
{
QmlPlugin *pPlugin = new QmlPlugin;//实例化类对象
pPlugin->ShowWindow();//调用类成员函数加载qml资源并显示窗口
}
在测试工程中调用导出的dll
1.定义函数指针
typedef void(*FUN1)();//定义函数指针
2.加载dll
//加载动态库
QLibrary lib(QCoreApplication::applicationDirPath() + "/lib/QmlPlugin.dll");
if (lib.load()) //加载dll成功
{
FUN1 pShow = (FUN1)lib.resolve("ShowWindowApp");
if (pShow) //成功找到函数
{
pShow();//调用函数
}
else //调用函数失败
{
qDebug() << "函数调用失败";
}
}
else //加载dll失败
{
qDebug() << "加载dll出错";
}
测试工程完整源码
QmlPluginTest.h
#pragma once
#include <QObject>
class QmlPluginTest : public QObject
{
Q_OBJECT
public:
QmlPluginTest();
Q_INVOKABLE void showWindow();
};
QmlPluginTest.cpp
#include "QmlPluginTest.h"
#include <QLibrary>
#include <QDebug>
#include <QCoreApplication>
QmlPluginTest::QmlPluginTest()
{
}
void QmlPluginTest::showWindow()
{
typedef void(*FUN1)();//定义函数指针
//加载动态库
QLibrary lib(QCoreApplication::applicationDirPath() + "/lib/QmlPlugin.dll");
if (lib.load()) //加载dll成功
{
FUN1 pShow = (FUN1)lib.resolve("ShowWindowApp");
if (pShow) //成功找到函数
{
pShow();//调用函数
}
else //调用函数失败
{
qDebug() << "函数调用失败";
}
}
else //加载dll失败
{
qDebug() << "加载dll出错";
}
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "QmlPluginTest.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QmlPluginTest qmlPluginTest;
engine.rootContext()->setContextProperty("qmlPluginTest", &qmlPluginTest);
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
return app.exec();
}
main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
ApplicationWindow {
id:applicationWindow
visible: true
width: 400
height: 300
title: qsTr("插件测试窗口")
Button{
id:button
text: qsTr("调用dll插件中的窗口")
height:48
width:250
anchors.centerIn: parent
MouseArea{
id:mouseArea
anchors.fill: parent
onPressed: {
qmlPluginTest.showWindow();
}
}
}
}