Echo Plugin Example的理解

通过插件使应用程序可扩展涉及以下步骤:

  1. 定义一组用于与插件对话的接口(仅具有纯虚函数的类)。
  2. 使用Q_DECLARE_INTERFACE()宏向Qt的元对象系统声明该接口。
  3. 在应用程序中使用QPluginLoader加载插件。
  4. 使用qobject_cast()测试插件是否实现了给定的接口。
class EchoInterface{ //! [1]
public:
    virtual ~EchoInterface() = default;
    virtual QString echo(const QString &message) = 0;
};
QT_BEGIN_NAMESPACE
#define EchoInterface_iid "org.qt-project.Qt.Examples.EchoInterface"
Q_DECLARE_INTERFACE(EchoInterface, EchoInterface_iid)//! [2]
QT_END_NAMESPACE
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));//! [3]
QObject *plugin = pluginLoader.instance();
if (plugin) {
    echoInterface = qobject_cast<EchoInterface *>(plugin);//! [4]
    if (echoInterface)
        return true;
    pluginLoader.unload();
}

编写一个插件的步骤:

  1. 声明一个插件类,该类继承自QObject和该插件要提供的接口。
  2. 使用Q_INTERFACES] ()宏告诉Qt的元对象系统有关接口的信息。
  3. 使用Q_PLUGIN_METADATA()宏导出插件。
  4. 使用合适的.pro文件构建插件。
class EchoPlugin : public QObject, EchoInterface{ //! [1]
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.Examples.EchoInterface" FILE "echoplugin.json") //! [2]
    Q_INTERFACES(EchoInterface)  //! [3]
public:
    QString echo(const QString &message) override;
};

[1]Qt插件创建及加载
[2]Qt插件中的几个宏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
D:\004 newCoinInfo>java -version java version "1.8.0_181" Java(TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode) D:\004 newCoinInfo>echo %JAVA_HOME% D:\JDK 但是 D:\004 newCoinInfo>mvn clean package [INFO] Scanning for projects... [INFO] [INFO] ------------------------< com.example:test_reg >------------------------ [INFO] Building test_reg 0.0.1-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- clean:3.2.0:clean (default-clean) @ test_reg --- [INFO] Deleting D:\004 newCoinInfo\target [INFO] [INFO] --- resources:3.2.0:resources (default-resources) @ test_reg --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Using 'UTF-8' encoding to copy filtered properties files. [INFO] Copying 1 resource [INFO] Copying 1 resource [INFO] [INFO] --- compiler:3.10.1:compile (default-compile) @ test_reg --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 7 source files to D:\004 newCoinInfo\target\classes [INFO] ------------------------------------------------------------- [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? [INFO] 1 error [INFO] ------------------------------------------------------------- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.914 s [INFO] Finished at: 2023-07-19T18:31:17+08:00 [INFO] ------------------------------------------------------------------------ [WARNING] [WARNING] Plugin validation issues were detected in 2 plugin(s) [WARNING] [WARNING] * org.apache.maven.plugins:maven-resources-plugin:3.2.0 [WARNING] * org.apache.maven.plugins:maven-compiler-plugin:3.10.1 [WARNING] [WARNING] For more or less details, use 'maven.plugin.validation' property with one of the values (case insensitive): [BRIEF, DEFAULT, VERBOSE] [WARNING] [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project test_reg: Compilation failure [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? [ERROR] [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException 报错
最新发布
07-20

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值