Qt-plugin creation and application reference

 

Qt-plugin creation reference

qt-plugin creation extension of functionality. Qt provides two APIs to create plug-ins: high-level APIs used to extend Qt itself, such as custom database drivers, image formats, text encoding and custom styles, etc.; low-level APIs used to extend Qt applications. If you want to write a plugin to extend Qt itself, you can subclass the appropriate plugin base class, then override some functions and add a macro. You can learn about this section by looking at the How to Create Qt Plugins keyword in the help, and here you can see the plugin base provided by Qthttps://blog.csdn.net/qq_35629971/article/details/124178763

Create an application plugin
When creating a plugin, you must first create an interface. An interface is a class that only contains pure virtual functions. The plugin class should inherit from this interface. Plugin classes are stored in a shared library, so they can be loaded while the application is running. Creating a plugin involves the following steps:

Define a plug-in class, which needs to inherit from both the QObject class and the interface class corresponding to the functions provided by the plug-in;
Register the interface in Qt's meta-object system using the Q_INTERFACES() macro;
Use the Q_PLUGIN_METADATA() macro to export the plugin;
Build the plugin with the appropriate .pro file.
Making an application extensible through plugins involves the following steps:

Define a set of interfaces (abstract classes with only pure virtual functions);

Register the interface in Qt's meta-object system using the Q_DECLARE_INTERFACE() macro;

Use QPluginLoader in your application to load plugins;

Use qobject_cast() to test whether a plugin implements a given interface.

The process of creating an application plugin is explained below by creating a plugin that filters the first number that appears in the string.

Two projects need to be created here, one project is used to generate the plug-in, that is, the dll file; the other project is a test program used to use the plug-in.

Because there are common files in these two projects, they are put into a directory here.
--------
 

#ifndef REGEXPPLUGIN_H
#define REGEXPPLUGIN_H
#include <QObject>
#include "regexpinterface.h"



class RegExpPlugin:public QObject,RegExpInterface
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qter.Examples.myplugin.RegExpInterface" FILE "myplugin.json")
    Q_INTERFACES(RegExpInterface)
public:
   // RegExpPlugin();
    QString regexp(const QString &message);
};

#endif // REGEXPPLUGIN_H
#include "regexpplugin.h"
#include <QRegExp>
#include <QtPlugin>

QString RegExpPlugin::regexp(const QString &message)
{
   QRegExp rx("\\d+");
   rx.indexIn(message);
   QString str=rx.cap(0);
   return str;
}

application:

#ifndef REGEXPINTERFACE_H
#define REGEXPINTERFACE_H
#include <QString>
#include <QtPlugin>


class RegExpInterface
{
public:
  //  RegExpInterface();
    virtual ~RegExpInterface(){}
    virtual QString regexp(const QString &message) = 0;
};
Q_DECLARE_INTERFACE(RegExpInterface,
                    "org.qter.Examples.myplugin.RegExpInterface")
#endif // REGEXPINTERFACE_H
#ifndef WIDGET_H
#define WIDGET_H
#include "regexpinterface.h"

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_pushButton_filter_clicked();

private:
    Ui::Widget *ui;
    RegExpInterface *regexpInterface2;
    bool loadPlugin();
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QPluginLoader>
#include <QMessageBox>
#include <QDir>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    if(!loadPlugin())
    {
        QMessageBox::information(this,"Error","could not load this plugin");
        ui->lineEdit->setEnabled(false);
        ui->pushButton_filter->setEnabled(false);
    }

}

Widget::~Widget()
{
    delete ui;
}

bool Widget::loadPlugin()
{
    //QDir pluginsDir("./plugins"); //exe file exe folder Debug and plugins are in the same directory, the current directory
    // QDir pluginsDir("../plugins");//The upper level directory of the Debug directory of the exe folder
      QDir pluginsDir("../../plugins");//The upper-level directory of the exe file exe folder Debug directory
    foreach(QString fileName,pluginsDir.entryList(QDir::Files))
    {
        QPluginLoader plugin_loader(pluginsDir.absoluteFilePath(fileName));
        QObject *plugin=plugin_loader.instance();
        if(plugin)
        {
            regexpInterface2 =qobject_cast<RegExpInterface *>(plugin);
            if(regexpInterface2)
            {
                return true;
            }
        }
    }
    return false;

}


void Widget::on_pushButton_filter_clicked()
{
    QString str=regexpInterface2->regexp(ui->lineEdit->text());
    ui->labelNum->setText(str);
}

Tags: Qt programming language

Posted by volomike on Sun, 02 Oct 2022 21:33:16 +0300