Qt5.12实战之在QWidget应用中调用动态链接库中的函数

调用演示:

 

1.使用win32 api方法动态加载dll并调用函数

HINSTANCE inst = NULL;//动态链接库的句柄

        //动态加载dll
        inst = LoadLibrary(_T("TestExport.dll"));
        if (!inst)
        {
            QMessageBox::information(this,NULL,QStringLiteral("加载TestExport.dll失败"));
        }
        else {
            //通过进程地址加载导出函数
            export_func = (_EXPORT_FUNC)GetProcAddress(inst, "export_func");//加载进程中的export_func函数
            if (!export_func)
            {
                QMessageBox::information(this,NULL,QStringLiteral("获取函数export_func地址失败"));
                //释放库
                FreeLibrary(inst);
            }
            else {
                export_func();//根据地址调用导出函数
            }
        }
        //释放库
        FreeLibrary(inst);

 2.使用QLibrary类加载dll,并调用函数

使用前先引用头文件:  #include <QLibrary>

QLibrary exportLib("TestExport");//导出库,使用QLibrary加载时不用加后缀名
    //加载导入库
    if(!exportLib.load()){
        QMessageBox::critical(this,NULL,QStringLiteral("使用QLibrary加载TextExport导出库失败"));
    }else{
        export_func = (_EXPORT_FUNC)exportLib.resolve("export_func");//取函数地址
        if(!export_func){
            QMessageBox::critical(this,NULL,QStringLiteral("调用TextExport导出库export_func函数失败"));
        }else{
            export_func();//调用导出库中的函数
            exportLib.unload();//释放库
        }
    }

3.通过.pro文件来加载,并调用函数

# 指定加载路径,调试运行时加载项目当前路径下的libs目录中的TestExport.lib TestExportDef.lib
# 直接运行程序时,加载程序当前路径上的TestExport.lib TestExportDef.lib
LIBS +=-L$$PWD/libs -lTestExport -lTestExportDef

复制库定义头文件到当前工程并添加到工程

复制: 

添加到工程: 

选择要添加的文件: 

 

添加成功: 

调用库中的函数:

 func2();
 func3(QByteArray("Hello").data(),666);

 ui文件中添加三个按钮与对按钮添加点击事件

事件处理代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <Windows.h>
#include <tchar.h>
#include <QLibrary>
#include "TestExportDef.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    HINSTANCE inst = NULL;//动态链接库的句柄

        //动态加载dll
        inst = LoadLibrary(_T("TestExport.dll"));
        if (!inst)
        {
            QMessageBox::information(this,NULL,QStringLiteral("加载TestExport.dll失败"));
        }
        else {
            //通过进程地址加载导出函数
            export_func = (_EXPORT_FUNC)GetProcAddress(inst, "export_func");//加载进程中的export_func函数
            if (!export_func)
            {
                QMessageBox::information(this,NULL,QStringLiteral("获取函数export_func地址失败"));
                //释放库
                FreeLibrary(inst);
            }
            else {
                export_func();//根据地址调用导出函数
            }
        }
        //释放库
        FreeLibrary(inst);
}


void MainWindow::on_pushButton_2_clicked()
{
    QLibrary exportLib("TestExport");//导出库,使用QLibrary加载时不用加后缀名
    //加载导入库
    if(!exportLib.load()){
        QMessageBox::critical(this,NULL,QStringLiteral("使用QLibrary加载TextExport导出库失败"));
    }else{
        export_func = (_EXPORT_FUNC)exportLib.resolve("export_func");//取函数地址
        if(!export_func){
            QMessageBox::critical(this,NULL,QStringLiteral("调用TextExport导出库export_func函数失败"));
            exportLib.unload();//释放库
        }else{
            export_func();//调用导出库中的函数
            exportLib.unload();//释放库
        }
    }

}


void MainWindow::on_pushButton_3_clicked()
{
    func2();
    func3(QByteArray("Hello").data(),666);
}

 导出库定义与实现:

 

#pragma once

#include "tchar.h"
int func1(const char *str, int x);//函数定义
void func2();//函数定义
int func3(char *str, int x);//函数定义
#include "windows.h"
#include "tchar.h"
#include "TestExportDef.h"

//函数实现
int func1(const char * str, int x)
{
	MessageBox(0, str, _T("函数func1"), 0);
	return x;
}

//函数实现
void func2()
{
	MessageBox(0, _T("没有参数的函数"), _T("函数func2"), 0);
}

int func3(char * str, int x)
{
	MessageBox(0, str, _T("函数func3"), 0);
	return x;
}

导出声明

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一段使用Qt5.12.12绘制播放音乐动态直方图的源代码: ```cpp #include <QtWidgets> class MusicWidget : public QWidget { public: MusicWidget(QWidget *parent = nullptr) : QWidget(parent) { setFixedSize(400, 300); } void setAudioData(const QVector<double> &data) { audioData = data; update(); } protected: void paintEvent(QPaintEvent *event) override { Q_UNUSED(event); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); int columnWidth = width() / audioData.size(); // 绘制直方图 for (int i = 0; i < audioData.size(); ++i) { int columnHeight = height() * audioData[i]; QPoint topLeft(i * columnWidth, height() - columnHeight); QColor columnColor(Qt::red); columnColor.setAlphaF(0.5); painter.setBrush(columnColor); painter.drawRect(QRect(topLeft, QSize(columnWidth, columnHeight))); } } private: QVector<double> audioData; }; int main(int argc, char *argv[]) { QApplication app(argc, argv); // 生成随机音频数据 QVector<double> audioData; for (int i = 0; i < 100; ++i) { double value = qrand() / static_cast<double>(RAND_MAX); audioData.append(value); } MusicWidget musicWidget; musicWidget.setAudioData(audioData); musicWidget.show(); return app.exec(); } ``` 这段代码创建了一个名为`MusicWidget`的QWidget子类,用于绘制动态直方图。在构造函数,设置了窗口的固定大小。`setAudioData`函数用于设置音频数据,当音频数据改变时,调用`update`函数触发重新绘制。 在`paintEvent`函数,通过QPainter类进行绘制操作。首先,设置了绘制抗锯齿效果。然后,根据音频数据的大小,计算出每个直方图列的宽度。着,使用循环遍历音频数据并绘制每个直方图列。绘制使用了Qt引擎的QPainter类的drawRect函数,根据数据值计算出直方图的高度,并设定矩形的位置和尺寸。绘制完成后,显示窗口。 在main函数,首先生成了一个随机音频数据,然后创建MusicWidget对象,并将音频数据设置到该对象,最后显示窗口并启动应用程序的事件循环。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林鸿群

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

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

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

打赏作者

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

抵扣说明:

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

余额充值