一:动态库
1.创建

SharedLibrary.pro

QT       += widgets

TARGET = SharedLibrary
TEMPLATE = lib

DEFINES += SHAREDLIBRARY_LIBRARY

SOURCES += sharedlibrary.cpp

HEADERS += sharedlibrary.h\

sharedlibrary.h

#include <QtCore/qglobal.h>

#if defined(SHAREDLIBRARY_LIBRARY)
#  define SHAREDLIBRARYSHARED_EXPORT Q_DECL_EXPORT
#else
#  define SHAREDLIBRARYSHARED_EXPORT Q_DECL_IMPORT
#endif

#include <QWidget>

class SHAREDLIBRARYSHARED_EXPORT SharedLibrary : public QWidget
{
    Q_OBJECT
public:
    explicit SharedLibrary(QWidget *parent = 0);
    void updateBackground();
    int subtract(int a, int b);

private slots:
    void onClicked();
};

extern "C" {
    SHAREDLIBRARYSHARED_EXPORT SharedLibrary* getSharedLibrary(); //获取类SharedLibrary对象
    SHAREDLIBRARYSHARED_EXPORT int add(int a, int b);  //自定义的外部函数
}

sharedlibrary.cpp

#include "sharedlibrary.h"

#include <QPushButton>
#include <QMessageBox>
#include <QTime>

SharedLibrary::SharedLibrary(QWidget *parent)
    : QWidget(parent)
{
    QPushButton *pButton = new QPushButton(this);
    pButton->setText("Test Shared Library");

    connect(pButton, SIGNAL(clicked()), this, SLOT(onClicked()));
}

void SharedLibrary::onClicked()
{
    QMessageBox::information(this, "Tip", "Shared Library...");
}

void SharedLibrary::updateBackground()
{
    QTime time;
    time = QTime::currentTime();
    qsrand(time.msec() + time.second()*1000);

    int nR = qrand() % 256;
    int nG = qrand() % 256;
    int nB = qrand() % 256;

    QPalette palette(this->palette());
    palette.setColor(QPalette::Background, QColor(nR, nG, nB));
    this->setPalette(palette);
}

int SharedLibrary::subtract(int a, int b)
{
    return a - b;
}

SharedLibrary* getSharedLibrary()
{
    return new SharedLibrary();
}

int add(int a, int b)
{
    return a + b;
}

2.使用
SharedLibraryTest.pro

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = SharedLibraryApplication
TEMPLATE = app

SOURCES += main.cpp

HEADERS  +=


win32:CONFIG(release, debug|release): LIBS += -L$$PWD/SharedLibrary/x86/release/ -lSharedLibrary
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/SharedLibrary/x86/debug/ -lSharedLibrary
else:unix: LIBS += -L$$PWD/SharedLibrary/x86/ -lSharedLibrary

INCLUDEPATH += $$PWD/SharedLibrary/x86/Debug
DEPENDPATH += $$PWD/SharedLibrary/x86/Debug

main.cpp

#include <QApplication>


#include "SharedLibrary/sharedlibrary.h"

#include <QDebug>
#include <QMessageBox>
#include <QLibrary>
//#include <>
typedef SharedLibrary* (*GetSharedLibrary)();//定义函数指针,获取类TestDLL对象;
typedef int (*Fun)(int, int); //定义函数指针,int add(int a,int b);

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    SharedLibrary w;
    w.resize(400, 200);
    w.updateBackground();
    w.show();

    int nSubtract = w.subtract(10, 4);
    int nAdd = add(5, 5);
    QMessageBox::information(NULL, "Tip", QString("subtract:%1  add:%2").arg(nSubtract).arg(nAdd));

//    //声明所用到的dll文件
//    QLibrary mylib("D:/WorkSpace/Qt Projects/SharedLibraryApplication/SharedLibrary/SharedLibrary.dll");
//    qDebug() << "mylib path: " << mylib.fileName();

//    //判断是否正确加载
//    if (mylib.load())
//    {
//        qDebug() << "library loaded";

//        GetSharedLibrary getSharedLibrary = (GetSharedLibrary)mylib.resolve("getSharedLibrary");
//        Fun add = (Fun)mylib.resolve("add");
//        if (getSharedLibrary)
//        {
//            SharedLibrary *pLib = getSharedLibrary();
//            pLib->updateBackground();
//            pLib->subtract(10, 4);
//            pLib->show();
//            pLib->setWindowTitle("Test ShareLibrary");
//        }
//        else
//        {
//            qDebug() << "Could not show widget from the loaded library";
//        }

//        if (add)
//        {
//            int nResult = add(5, 5);
//            QMessageBox::information(NULL, "Tip", QString::number(nResult));
//        }
//        else
//        {
//            qDebug() << "Could not add from the loaded library";
//        }
//    }
//    //加载失败
//    else
//    {
//        qDebug() << mylib.errorString();
//    }

    return a.exec();
}

注意点:Qt生成的动态库不能提供给VS使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值