qt生成dll插件并调用dll插件中的窗口

 演示效果:

 

 生成生态库配置

#使用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();
            }
        }
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林鸿群

有你的鼓励,我会更加努力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值