当前位置:网站首页>Qt5 plug-in production

Qt5 plug-in production

2022-06-12 13:33:00 You stole my name

One 、Qt Plug-in mechanism

1、Qt Plug in introduction

A plug-in is a program written according to a certain standard application program interface , It aims to develop programs that realize functions that the application software platform does not have .

2、Qt plug-in unit API

Qt Two types are available API Used to create plug-ins : One is higher order API, Used to extend Qt Its own function , Such as custom database driver , Image format , Text encoding , Custom styles, etc ; One is low order API, Used to extend Qt Applications .

3、 Extend application functions through plug-ins

A、 Define an interface set ( Classes with only pure virtual functions ), Used to communicate with plug-ins .
B、 Using macros Q_DECLARE_INTERFACE() Tell the interface to Qt Meta object system .
C、 Used in applications QPluginLoader To load the plug-in .
D、 Using macros qobject_cast() To determine whether a plug-in implements the interface .

4、 Creating plug-ins

The steps to create a plug-in are as follows :
A、 Declare the plug-in class , The plug-in class inherits from QObject Interface and plug-in implementation .
B、 Using macros Q_INTERFACES() Tell... About the plug-in interface Qt Meta object system .
C、 Using macros Q_EXPORT_PLUGIN2() Export plug-in class .
D、 Use appropriate .pro File building plug-ins .
Before loading the plug-in , QCoreApplication Object must be initialized .

Two 、 Plug in development examples

1、 Create a project

 Insert picture description here

2、 establish GUI Application engineering

 Insert picture description here
 Insert picture description here
Here you go QWidget And QMainWindow It doesn't matter
 Insert picture description here
Choose the compiler you have installed
 Insert picture description here

2、 Create a plug-in interface base class

stay MyWidget Add an interface to the application MyfirstPlugin.h. The contents are as follows :

#ifndef MYFIRSTPLUGIN_H
#define MYFIRSTPLUGIN_H
#include<QObject>
class MyfirstPlugin
{
    
public:
    virtual ~MyfirstPlugin(){
    }
    virtual QString name()=0;
};
#define MYPLUGININTERFACE_IID "com.FirstPlugin.myfirstInterface"
Q_DECLARE_INTERFACE(MyfirstPlugin, MYPLUGININTERFACE_IID)

#endif // MYFIRSTPLUGIN_H

MYPLUGININTERFACE_IID It should be a unique string with similar package name format , You can do it according to your own preferences . Be careful : A pure virtual destructor must be defined , And all functions of this interface must be virtual functions .

3、 Create Library Project

stay First Add a sub project named MyPlugin Sub library project of .
 Insert picture description here
Pay attention to CONFIG Add plugin, It's to tell Qt This is a plug-in , At the same time, load the path of the interface file made in the previous step .
 Insert picture description here
…// I'll see if anyone reads it , When someone reads it, I continue to write ./

原网站

版权声明
本文为[You stole my name]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010515501801.html