1、核心插件

1.1、插件pro文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
include(../../plugins.pri)

QT += widgets

DEFINES += COREPLUGIN_LIBRARY

TARGET = coreplugin

LIBS += \
-lextensionsystem

HEADERS += \
coreplugin.h

SOURCES += \
coreplugin.cpp

DISTFILES += \
coreplugin.json



这里只实现一个简单的插件,该插件依赖 extensionsystem,因此LIBS添加extensionsystem库; coreplugin.json 文件为插件的描述文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
{
"Name" : "CorePlugin",
"Version" : "0.0.1" ,
"CompatVersion" : "0.0.1",
"Required" : true,
"Vendor" : "MakerInChina" ,
"Copyright" : "(C) 2021 The Qt Inc" ,
"License" : "GPL",
"Category" : "Core" ,
"Description" : "MainWindow"
}
1.2、定义插件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#ifndef COREPLUGIN_H
#define COREPLUGIN_H

#include <QObject>
#include <extensionsystem/iplugin.h>

#include <QMainWindow>

class CorePlugin : public ExtensionSystem::IPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "monkeyqdk.qtc.plugin" FILE "coreplugin.json")
public:
explicit CorePlugin(){}

~CorePlugin(){}

bool initialize(const QStringList &arguments, QString *errorString) override;
void extensionsInitialized() override;
QObject* remoteCommand(const QStringList &, const QString &, const QStringList &) override;

private:
QScopedPointer<QMainWindow> m_mainWindow;

signals:

};

#endif // COREPLUGIN_H

  • 插件需要继承 ExtensionSystem::IPlugin 插件接口;
  • Q_PLUGIN_METADATA 定义插件的IID和插件描述文件;
  • 重写initialize、extensionsInitialized、remoteCommand 三个函数;
1.3、实现插件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "coreplugin.h"

#include <QLabel>
#include <QDebug>

bool CorePlugin::initialize(const QStringList &, QString *)
{

qDebug()<<__FILE__<<" at line "<<__LINE__<<" :"<<" coreplugin initialize";

QMainWindow *mwin = new QMainWindow;

QLabel *label = new QLabel(mwin);
label->setText("corePlugin");
mwin->setCentralWidget(label);
mwin->setMinimumSize(800,400);
mwin->setWindowTitle("MonkeyQDK 0.01 by MakerInChina");
m_mainWindow.reset(mwin);

return true;
}

void CorePlugin::extensionsInitialized()
{
// m_mainWindow->extensionsInitialized();
}

QObject *CorePlugin::remoteCommand(const QStringList &, const QString &, const QStringList &)
{
m_mainWindow->show();

return nullptr;
}

  • initialize 函数中创建一个主窗口;
  • remoteCommand函数中显示窗口;