欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QPluginLoader 构建插件系统 基本控件(二十六)

程序员文章站 2022-05-28 11:35:07
...

一、说明
当我们的程序逐渐变大时,我们可能就会考虑功能插件化,或者支持某些模块动态加载和卸载。
二、代码
1.插件系统软件框架
QPluginLoader 构建插件系统 基本控件(二十六)
2.homeFrameMenuBarInterface.h

#ifndef HOMEFRAMEMENUBARINTERFACE_H
#define HOMEFRAMEMENUBARINTERFACE_H

#include <QWidget>
#include "menuBarInterface.h"

class homeFrameMenuBarInterface : public QObject,public menuBarInterface
{
    Q_OBJECT
public:
    explicit homeFrameMenuBarInterface(QObject *parent = nullptr);

    Q_INTERFACES(menuBarInterface)
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.homeFrameMenuBarInterface" FILE "homeFrameMenuBarInterface.json")

    QWidget* create();
};

#endif // HOMEFRAMEMENUBARINTERFACE_H


3.menuBarInterface.h

#ifndef MENUBARINTERFACE_H
#define MENUBARINTERFACE_H
#include <QObject>
class menuBarInterface
{
public:
    virtual ~menuBarInterface(){}
    virtual QWidget* create() = 0;
};
Q_DECLARE_INTERFACE(menuBarInterface, "org.qt-project.Qt.menuBarInterface");

#endif // MENUBARINTERFACE_H

4.homePage.cpp

void homePage::loadPlguins()
{
    QDir dir;
    dir.setPath(QCoreApplication::applicationDirPath());
    foreach (QString fileName, dir.entryList(QDir::Files)){
        QPluginLoader pluginLoader(dir.absoluteFilePath(fileName));
        QObject *plugin = pluginLoader.instance();
        if (plugin){
            if(QString(plugin->metaObject()->className()) == "homeFrameMenuBarInterface"){
                menuBarInterface *interface = qobject_cast<menuBarInterface *>(plugin);
                if(interface){
                    m_pHomePageMenuBar = interface->create();
                }
            }
        }
    }
}

5.homeFrameMenuBarInterface.pro

QT += core gui

TEMPLATE = lib
CONFIG += plugin

CONFIG += c++11
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    homeframemenubarinterface.cpp \
    menubar.cpp

HEADERS += \
    homeframemenubarinterface.h \
    menuBarInterface.h \
    menubar.h

DISTFILES += homeFrameMenuBar.json

# Default rules for deployment.
unix {
    target.path = $$[QT_INSTALL_PLUGINS]/generic
}
!isEmpty(target.path): INSTALLS += target


CONFIG(debug, debug|release){
    DLLDESTDIR = ../../../Target/debug
    TARGET = $$join(TARGET,,,d)
}
CONFIG(release, debug|release){
    DLLDESTDIR = ../../../Target/release
}

三、效果图
QPluginLoader 构建插件系统 基本控件(二十六)
QPluginLoader 构建插件系统 基本控件(二十六)

相关标签: # Qt-基本控件