Qt implements plug-in development step by step (with source code)

Source: Internet
Author: User

I started to study QT from the end of last month because the unified platform for the new project needs to be developed using QT. However, I have never felt that I am getting started. I don't know why, but I have implemented many functions, however, I don't know why, just like the current project.

I used plug-ins for development. I generally know what plug-in-type development is used for software development. I used C # in the past, but QT plug-in development was the first time. Baidu has finally implemented it for a long time. Here we will record it! Here we will not talk about the principle, but we will talk about the process directly...

Part 1

Build a plug-in program to implement two functions

(1) Create an addition function for the main program to dynamically call

(2) create a query page for the main program to dynamically call

Step 1 create a plug-in program

Open the File menu-new file or project-select other project-C ++ Library

Click choose to enter

 

Select shared library-enter name-select your project location

 

Click Next to select the required project module and click Next to complete

You will find that the project has two header files and one source file.

Click Create File to add a new file.

Choose Create File> QT design class> dialog> input name

Header file

Mytestdll_global_h

View code

Myshowtest_h

View code

#ifndef MYSHOWTEST_H#define MYSHOWTEST_H#include <QDialog>namespace Ui {    class MyShowTest;}class MyShowTest : public QDialog{    Q_OBJECTpublic:    explicit MyShowTest(QWidget *parent = 0);    ~MyShowTest();private:    Ui::MyShowTest *ui;};#endif // MYSHOWTEST_H

Mytestdll_h

View code

#ifndef MYTESTDLL_H#define MYTESTDLL_H#include "MyTestDll_global.h"#include "qdialog.h"class MYTESTDLLSHARED_EXPORT MyTestDll {public:    MyTestDll();};extern "C" Q_DECL_EXPORT int add(int a,int b);extern "C" Q_DECL_EXPORT QDialog *showDialog();#endif // MYTESTDLL_H

Source File
Myshowtest. cpp

View code

#include "myshowtest.h"#include "ui_myshowtest.h"MyShowTest::MyShowTest(QWidget *parent) :    QDialog(parent),    ui(new Ui::MyShowTest){    ui->setupUi(this);}MyShowTest::~MyShowTest(){    delete ui;}

Mytestdll. cpp

View code

#include "mytestdll.h"#include "MyShowTest.h"MyTestDll::MyTestDll(){}int add(int a,int b){  return a+b;}QDialog *showDialog(){    MyShowTest *ss=new MyShowTest();    return ss;}

UI

Click build project to complete the plug-in.

Part 2

Create a main program

Add the generated plug-in mytestdll.dllto the same directory as mytest.exe.

Header file

View code

#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>namespace Ui {    class MainWindow;}class MainWindow : public QMainWindow{    Q_OBJECTpublic:    explicit MainWindow(QWidget *parent = 0);    ~MainWindow();private:    Ui::MainWindow *ui;private slots:    void on_pushButton_2_clicked();    void on_pushButton_clicked();};#endif // MAINWINDOW_H

Source File

View Code view code

# Include "mainwindow. H "# include" ui_main1_1_h "# include" qlibrary. H "# include" qmessagebox. H "# include" string. H "mainwindow: mainwindow (qwidget * parent): qmainwindow (parent), UI (new UI: mainwindow) {UI-> setupui (this);} mainwindow ::~ Mainwindow () {Delete UI;} typedef int (* Fun) (INT, INT); // defines the function pointer for calling void mainwindow: on_pushbutton_clicked () {qlibrary mylib ("mytestdll. DLL "); // declare the DLL file int result; qstring sresult; If (mylib. load () // determine whether to load {fun open = (fun) mylib. resolve ("add"); // invoke the add () function if (open) // whether the connection is successful add () function {int S1 = This-> UI-> lineedit-> text (). toint (); int S2 = This-> UI-> lineedit_2-> text (). toint (); Result = open (S1, S 2); // here the function pointer calls the add () function sresult = qstring: Number (result) in the DLL ); this-> UI-> lineedit_3-> settext (sresult) ;}} else {qmessagebox: Information (null, "no", "DLL is not loaded! ") ;}} Typedef qdialog * (* funs) (); // define the function pointer for calling void mainwindow: on_pushbutton_2_clicked () {// load the plug-in qlibrary mylib ("mytestdll. DLL "); // declare the DLL file used if (mylib. load () // determine whether {funs open = (funs) mylib is correctly loaded. resolve ("showdialog"); // invoke the add () function if (open) // whether the add () function is successfully connected {qdialog * form1 = open (); form1-> show ();}}}

Effect

Complete the reservation for 2 Functions

Source code download: http://download.csdn.net/detail/huangyuancao/5023059

PS: How does the blog garden provide source code download...

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.