CTK插件-入门打印
一、生成插件
0、環(huán)境說明
編譯ctk:Qt5.12.2+MSVC2017_64+Cmake
生成插件:Qt5.12.2+MSVC2017_64+qmake
1、服務類,純虛類,提供接口
abslogservice.h
#ifndef ABSLOGSERVICE_H
#define ABSLOGSERVICE_H
#include <QObject>
class AbsLogService{
public:
virtual ~AbsLogService(){}
virtual void log(QString info) = 0;
};
Q_DECLARE_INTERFACE(AbsLogService,"judesmorning.zxy.AbsLogService")
#endif // ABSLOGSERVICE_H
2、實現(xiàn)插件類,實現(xiàn)純虛函數(shù)
logservice.h
#ifndef LOGSERVICE_H
#define LOGSERVICE_H
#include <QObject>
#include "abslogservice.h"
class ctkPluginContext;
class LogService : public QObject, public AbsLogService
{
Q_OBJECT
Q_INTERFACES(AbsLogService)//表名當前插件類實現(xiàn)這個服務接口
public:
LogService(ctkPluginContext* context);
void log(QString info);
};
#endif // LOGSERVICE_H
logservice.cpp
#include "logservice.h"
#include <QDebug>
#include "ctkPluginContext.h"
#include <QTime>
LogService::LogService(ctkPluginContext* context)
{
context->registerService<AbsLogService>(this);
}
void LogService::log(QString info)
{
qDebug()<<QTime::currentTime().toString("HH:mm:ss ") + info;//打印加上時間戳
}
3、激活插件,加入ctk框架的生命周期中
firstpluginactivator.h
#ifndef FIRSTPLUGINACTIVATOR_H
#define FIRSTPLUGINACTIVATOR_H
#include <QObject>
#include "ctkPluginActivator.h"
#include "ctkPluginContext.h"
#include "logservice.h"
class FirstPluginActivator : public QObject, public ctkPluginActivator
{
Q_OBJECT
Q_INTERFACES(ctkPluginActivator)
Q_PLUGIN_METADATA(IID "LogPlugin")
public:
FirstPluginActivator();
void start(ctkPluginContext *context);
void stop(ctkPluginContext *context);
private:
QScopedPointer<AbsLogService> m_log;//智能指針,自動析構(gòu)回收
};
#endif // FIRSTPLUGINACTIVATOR_H
firstpluginactivator.cpp
#include "firstpluginactivator.h"
#include <QDebug>
FirstPluginActivator::FirstPluginActivator()
{
}
void FirstPluginActivator::start(ctkPluginContext *context)
{
qDebug() << "my plugin start";
m_log.reset(new LogService(context));
}
void FirstPluginActivator::stop(ctkPluginContext *context)
{
qDebug() << "my plugin stop";
Q_UNUSED(context)
}
4、添加資源文件
資源名隨便去,前綴為:TARGET/META_INF,文件名為:MANIFEST.MF
MANIFEST.MF內(nèi)容:
有些教程上說是前綴是工程名/META_INF,經(jīng)過測試工程名、MANIFEST.MF中的符號名、IID中的字符串都可以隨意取,只需要滿足這里的前綴是:TARGET/META_INF格式
有很多可填內(nèi)容,在ctkPluginConstants.h中能找到相關宏定義
5、.pro文件
QT += core
QT -= gui
TARGET = ctk-plugin-first
TEMPLATE = lib
CONFIG += plugin
#ctk源碼路徑
INCLUDEPATH += $$PWD/include/Core
+= $$PWD/include/PluginFramework
#ctk安裝路徑,主要用到ctkPluginFrameworkExport.h等
INCLUDEPATH += C:/Users/Administrator/Desktop/new/CTK-master/build_msvc2017/CTK-build/Libs/PluginFramework
INCLUDEPATH += C:/Users/Administrator/Desktop/new/CTK-master/build_msvc2017/CTK-build/Libs/Core
#ctk的動態(tài)庫路徑
LIBS += -LC:/Users/Administrator/Desktop/new/CTK-master/build_msvc2017/CTK-build/bin/Debug -lCTKCore -lCTKPluginFramework
HEADERS +=
firstpluginactivator.h
logservice.h
abslogservice.h
SOURCES +=
firstpluginactivator.cpp
logservice.cpp
RESOURCES +=
resource.qrc
注意:target的名字最好和工程名一致,不然可能出現(xiàn)devicenotopen錯誤
6、編譯生成插件
二、使用此插件
1、main.cpp
#include <QCoreApplication> #include <ctkPluginFrameworkFactory.h> #include <ctkPluginFramework.h> #include <ctkPluginException.h> #include <ctkPluginContext.h> #include <QtDebug> #include <QUrl> #include "abslogservice.h"
//插件路徑 QString static firstPlugin_filePath = "C:/Users/Administrator/Desktop/myctk2017/ctk-plugin-first/debug/ctk-plugin-first.dll"; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); ctkPluginFrameworkFactory frameworkFactory; QSharedPointer<ctkPluginFramework> framework = frameworkFactory.getFramework(); // 初始化并啟動插件框架 try { framework->init(); framework->start(); qDebug() << "CTK plugin framework start..."; } catch (const ctkPluginException &e) { qDebug() << "CTK plugin framework init err: " << e.what(); return -1; } // 獲取插件服務的contex ctkPluginContext* pluginContext = framework->getPluginContext(); try { // 安裝插件 QSharedPointer<ctkPlugin> plugin = pluginContext->installPlugin(QUrl::fromLocalFile(firstPlugin_filePath)); qDebug() << QString("Plugin[%1_%2] installed...").arg(plugin->getSymbolicName()).arg(plugin->getVersion().toString()); // 啟動插件 plugin->start(ctkPlugin::START_TRANSIENT); qDebug() << "Plugin start..."; } catch (const ctkPluginException &e) { qDebug() << QString("Failed install or run plugin: ") << e.what(); return -2; } // 獲取服務引用 ctkServiceReference reference = pluginContext->getServiceReference<AbsLogService>(); if (reference) { // 獲取指定 ctkServiceReference 引用的服務對象 AbsLogService* service = qobject_cast<AbsLogService*>(pluginContext->getService(reference)); if (service != Q_NULLPTR) { // 調(diào)用服務 service->log("hello hhh.."); } } return a.exec(); }
2、.pro
QT -= gui
CONFIG += console
CONFIG -= app_bundle
INCLUDEPATH += $$PWD/include/Core
+= $$PWD/include/PluginFramework
+= $$PWD/include/mydll #存放插件的服務接口類的頭文件
#ctk的源碼
INCLUDEPATH += C:/Users/Administrator/Desktop/new/CTK-master/build_msvc2017/CTK-build/Libs/PluginFramework
INCLUDEPATH += C:/Users/Administrator/Desktop/new/CTK-master/build_msvc2017/CTK-build/Libs/Core
#ctk的動態(tài)庫
LIBS += -LC:/Users/Administrator/Desktop/new/CTK-master/build_msvc2017/CTK-build/bin/Debug -lCTKCore -lCTKPluginFramework
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES +=
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
注意在ctk環(huán)境ok的情況下,只需要用到插件的額dll和插件的純虛類頭文件
3、運行
ps:附上學習ctk的流程
1、編譯CTK【可用現(xiàn)成的】
2、編譯插件:日志插件【用打印代替存儲日志】
3、新建工程:使用日志插件,并調(diào)用接口記錄一句話
4、插件的依賴測試:編寫日志插件和打印插件,日志插件依賴打印插件
5、編寫兩個插件通信:兩種通信方式都需要使用
6、編寫插件:使用服務工廠獲取服務
7、編寫插件:監(jiān)聽CTK所有事件
8、編寫插件:使用服務追蹤獲取服務
總結(jié)
以上是生活随笔為你收集整理的CTK插件-入门打印的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: !!!css如何让img图片居中?css
- 下一篇: 数据恢复(Data recovery)