Qt6 QML Book/扩展QML/插件内容

本文探讨如何在QML中构建一个FileIO插件,提供read和write功能,并通过C++接口与QML交互。我们将学习如何使用QQmlExtensionPlugin,注册自定义类型,并理解模块与插件的一对一关系。
摘要由CSDN通过智能技术生成

Plugin Content

插件内容

A plugin is a library with a defined interface, which is loaded on demand. This differs from a library as a library is linked and loaded on startup of the application. In the QML case, the interface is called QQmlExtensionPlugin. There are two methods interesting for us initializeEngine() and registerTypes(). When the plugin is loaded first the initializeEngine() is called, which allows us to access the engine to expose plugin objects to the root context. In the majority, you will only use the registerTypes() method. This allows you to register your custom QML types with the engine on the provided URL.

插件是一个带有定义接口的库,可以按需加载。这与库不同,因为库是在应用程序启动时链接和加载的。在QML情况下,该接口称为QQmlExtensionPlugin。有两种方法让我们感兴趣:initializeEngine()和registerTypes()。首先加载插件时,会调用initializeEngine(),这允许我们访问引擎,将插件对象暴露到根上下文中。在大多数情况下,您将只使用registerTypes()方法。这允许您在提供的URL上向引擎注册自定义QML类型。

Let us explore by building a small FileIO utility class. It would let you read and write text files from QML. A first iteration could look like this in a mocked QML implementation.

让我们通过构建一个小的FileIO实用程序类进行探索。它可以让你读写QML中的文本文件。在模拟的QML实现中,第一次迭代可能是这样的。

// FileIO.qml (good)
QtObject {
    function write(path, text) {};
    function read(path) { return "TEXT" }
}

This is a pure QML implementation of a possible C++ based QML API. We use this to explore the API. We see we need a read and a write function. We also see that the write function takes a path and a text, while the read function takes a path and returns a text. As it looks, path and text are common parameters and maybe we can extract them as properties to make the API easier to use in a declarative context.

这是一个纯QML实现的一个可能基于C++的QML API。我们用它来探索API。我们看到我们需要一个读read、write函数。我们还看到,write函数使用了路径path和文本text,而read函数使用了路径path并返回文本text。看起来,路径path和文本text是常见的参数,也许我们可以将它们提取为属性,使API更易于在声明性上下文中使用。

// FileIO.qml (better)
QtObject {
    property url source
    property string text
    function write() {} // open file and write text 
    function read() {} // read file and assign to text 
}

Yes, this looks more like a QML API. We use properties to allow our environment to bind to our properties and react to changes.

是的,这看起来更像一个QML API。我们使用属性来允许我们的环境绑定到我们的属性,并对变化做出反应。

To create this API in C++ we would need to create an Qt C++ interface looking like this.

要在C++中创建这个API,我们需要创建一个Qt C++接口,看起来像这样。

class FileIO : public QObject {
    ...
    Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
    Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
    ...
public:
    Q_INVOKABLE void read();
    Q_INVOKABLE void write();
    ...
}

The FileIO type need to be registered with the QML engine. We want to use it under the “org.example.io” module

FileIO类型需要在QML引擎中注册。我们想在“org.example.io”模块下使用它

import org.example.io 1.0

FileIO {
}

A plugin could expose several types with the same module. But it can not expose several modules from one plugin. So there is a one to one relationship between modules and plugins. This relationship is expressed by the module identifier.

一个插件可以使用同一个模块公开多个类型。但它不能从一个插件中公开多个模块。因此,模块和插件之间存在一对一的关系。这种关系由模块标识符表示。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值