Record the creation and use of the QT5 dynamic link library
Complete code at the end of the article for download
1. Create a dynamic link library
Create a new library item first
Select chose to go to the next page, type select Shared Library, enter a name: I'm typing a SLD.
Then click Next to
If we need qtgui here, we'll pick it up.
Then click Next until you finish
We add in the. Pro file
DESTDIR =.. \mydebug
It means that we put the generated content in this folder, and if it does not, it will automatically generate
Click on the item to remove the shadow build check box
After the setup is complete, we implement the method test.
Change the sld.h into this.
#ifndef sld_h#define sld_h#include "sld_global.h" #include <qstring>class sldshared_export sld{public: SLD ( ); QString getstr ();}; #endif
It's. cpp
#include "sld.h" #include <qdebug>sld::sld () { qdebug () << "";} QString Sld::getstr () { return "AA";}
We got one. Test method return AA
We'll add a new form for testing
I named Mydialog.
Modify the. h file to
#ifndef mydialog_h#define mydialog_h#include <QDialog> #include "sld_global.h" namespace Ui {class Mydialog;} Class Sldshared_export Mydialog:public qdialog{ q_objectpublic: explicit Mydialog (Qwidget *parent = 0); ~mydialog ();p rivate: ui::mydialog *ui;}; #endif//Mydialog_h
Cpp
#include "mydialog.h" #include "ui_mydialog.h" Mydialog::mydialog (Qwidget *parent): qdialog (Parent), UI (new Ui::mydialog) { ui->setupui (this);} Mydialog::~mydialog () { Delete ui;}
There's nothing to do here, just add sldshared_export.
Okay, that's it, we build it, we'll build our DLL in the Mydebug folder.
2. How to use the Dynamic link library
Create a new QT Widgetsapplication project
In the. Pro file, add the same as above
DESTDIR =.. \mydebug Click on the project to remove the shadow build
Plus
Includepath + =. /sldlibs + =-l$ $DESTDIR-lsld
The first sentence is to include the SLD item's folder in this project so that we can directly use the header file in it.
The second sentence is to tell the compiler where Lib is (I'm using the VS compiler if you can add sld.dll directly with MinGW)
I'm kind of adding a button to the form and add a slot
. h
#ifndef mainwindow_h#define mainwindow_h#include <qmainwindow>namespace Ui {class MAINWINDOW;} Class Mainwindow:public qmainwindow{ q_objectpublic: explicit MainWindow (Qwidget *parent = 0); ~mainwindow ();p rivate slots: void on_pushbutton_clicked ();p rivate: ui::mainwindow *ui;}; #endif//Mainwindow_h
. cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include "sld.h" #include <QMessageBox> #include "mydialog.h" Mainwindow::mainwindow (Qwidget *parent): Qmainwindow (Parent), UI (new Ui::mainwindow) { ui->setupui (this);} Mainwindow::~mainwindow () { Delete ui;} void mainwindow::on_pushbutton_clicked () { Mydialog * my =new mydialog (this); My->show (); Qmessagebox * Msg=new Qmessagebox (this); Sld s; Msg->settext (S.getstr ()); Msg->show ();}
Well, build, run and see the effect.
Source code: sharelibdemo.7z
Http://www.cnblogs.com/li-peng/p/3844614.htmlbv
QT5 the creation and use of dynamic link libraries (QT makes the dynamic library for its own use)