1.创建子项目project.即主项目名称。
2.创建带界面的子项目app.pro。用于主界面显示。
2.1 创建注意事项
选择Application------Qt Widgets Application
界面继承 QMainWindow
子项目添加到 project.pro(主项目目录下)
2.2.在带界面的子项目中 设置目录路径
在app.pro文件中添加如下语句:
CONFIG(debug,debug|release){
DESTDIR = ../bin/MyDebug
LIBS += -L../bin/MyDebug
}else{
DESTDIR = ../bin/MyRelease
LIBS += -L../bin/MyRelease
}
2.3.更改构建目录
3.创建dll子项目
3.1.注意事项
选择 其他项目 ------子目录项目
子项目添加到 project.pro(主项目目录下)
创建成功后,仍取消。
3.2 创建共享库(带界面) testDll.pro
3.3 共享库(带界面) testDll.pro 添加路径设置
在testDll.pro文件中添加如下代码
CONFIG(debug,debug|release){
DESTDIR += ../../bin/MyDebug
}else{
DESTDIR += ../../bin/MyRelease
}
3.4 引用dll 需在app.pro添加如下代码
INCLUDEPATH += ../dll/testDll
LIBS += -ltestDll
3.5 编译后,更改运行目录
3.6 添加新文件 testDll.ui文件
继承QDialog。
3.7更改 testDll.h testDll.cpp文件
testDll.h如下:
#ifndef TESTDLL_H
#define TESTDLL_H
#include <QDialog>
#include "testdll_global.h"
namespace Ui {
class TestDll;
}
class TESTDLLSHARED_EXPORT TestDll : public QDialog
{
Q_OBJECT
public:
explicit TestDll(QWidget *parent = 0);
~TestDll();
int GetTestdll();
void SetTestFormShow();
private:
Ui::TestDll *ui;
};
#endif // TESTDLL_H
testDll.cpp如下:
#include "testdll.h"
#include "ui_testDll.h"
TestDll::TestDll(QWidget *parent) :
QDialog(parent),
ui(new Ui::TestDll)
{
ui->setupUi(this);
}
TestDll::~TestDll()
{
delete ui;
}
int TestDll::GetTestdll()
{
return 30;
}
void TestDll::SetTestFormShow()
{
this->show();
}
3.8 在主界面程序中直接引用即可
#include "app.h"
#include "ui_app.h"
#include "testdll.h"
#include <QDebug>
app::app(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::app)
{
ui->setupUi(this);
}
app::~app()
{
delete ui;
}
void app::on_pushButton_clicked()
{
TestDll *pTestdll = new TestDll();
qDebug() << pTestdll->GetTestdll();
pTestdll->SetTestFormShow();
}
4.最终简易效果如下:
注:界面效果可自行随意设置
dll