Qt远程对象入门

Getting Started with Qt Remote Objects

Qt远程对象入门

Introduction

入门

The Qt Remote Objects module provides an easy way to share Qt APIs between processes and devices. For this to work, we require a data channel between processes and devices. To establish this data channel, first, you need a QRemoteObjectNode.

​Qt远程对象模块提供了一种在进程和设备之间共享Qt API的简单方法。为了实现这一点,我们需要在进程和设备之间建立数据通道。要建立此数据通道,首先需要一个QRemoteObjectNode。

In QtRO, a node is an endpoint for communication. In a remote objects network, each participant, be it a process or a device, needs its own node. QtRO is a peer-to-peer network, with connected nodes being the links in the network.

在QtRO中,节点是通信的端点。在远程对象网络中,每个参与者,无论是进程还是设备,都需要自己的节点。QtRO是一个对等网络,连接的节点是网络中的链接。

Nodes, by themselves, don’t provide much use. But their value comes when you add QObject classes to a node to share. Then, any peer node can request a copy or instance of the shared object from the host node, the node that shares it.

​节点本身并没有多大用处。但是,当您将QObject类添加到要共享的节点时,它们的价值就来了。然后,任何对等节点都可以从共享共享对象的主机节点请求共享对象的副本或实例。

Unlike when using normal class instances (with independent properties and signal emissions), QtRO automatically synchronizes changes to the shared object across all of its copies. With a few exceptions, these copies have the identical Qt API as the original object, and are meant to be used exactly as if the original object were available.

与使用普通类实例(具有独立属性和信号发射)不同,QtRO会自动同步所有副本中共享对象的更改。除了少数例外,这些副本具有与原始对象相同的Qt API,并且应该像使用原始对象一样使用。

In QtRO, the original object is called the Source. It's a fully-implemented C++ class, with the necessary business logic to provide the required functionality. Copies of this object are called Replicas. You don’t need to write any C++ code for a replica; you request an instance from a node instead. While you do need some code to use the replica, such as connecting signals to your own slots, you don’t need to implement the internal behavior – that's already done for you in the source.

​在QtRO中,原始对象称为Source。它是一个完全实现的C++类,具有提供所需功能所需的业务逻辑。此对象的副本称为Replica。不需要为副本编写任何C++代码;改为从节点请求实例。虽然您确实需要一些代码来使用副本,例如将信号连接到自己的插槽,但不需要实现内部行为——这在源代码中已经完成了。

Because the source can be in a different process or even on a different device, there are factors in QtRO that you need to consider, which you typically wouldn't run into when developing without Inter-Process Communication (IPC). Specifically, what happens if the process or device isn't there? This is where the additions to the Replica API come in:

因为源代码可能在不同的进程中,甚至在不同的设备上,所以QtRO中有一些因素需要考虑,如果没有进程间通信(IPC),在开发时通常不会遇到这些因素。具体来说,如果进程或设备不存在,会发生什么?这就是添加到副本API的地方:

  • The initialized() signal is emitted once the replica has received the source state from the QtRO network.
  • ​一旦副本从QtRO网络接收到源状态,就会发出initialized()信号。
  • Both the isReplicaValid property and the stateChanged() signal alert you if the connection is lost.
  • ​如果连接丢失,isReplicaValid属性和stateChanged()信号都会提醒。

Objects shared over QtRO use the links (conduits) between nodes for all communication. If you want to share a QObject, you must create a host node with a URL other nodes can connect to. You can also use the Registry to facilitate connections, but your nodes that share sources still need to be host nodes. Each shared object is given a name (a QString), used to identify it on the QtRO network.

​QtRO上共享的对象使用节点之间的链接(管道)进行所有通信。如果要共享QObject,则必须使用其他节点可以连接到的URL创建一个主机节点。也可以使用注册表来促进连接,但共享源的节点仍然需要是主机节点。每个共享对象都有一个名称(QString),用于在QtRO网络上标识它。

Implementation

实现

To illustrate the use of remote objects, on the source side, we need to:

为了说明远程对象的使用,在源端,我们需要:

1.Create the Source object that is replicated to other nodes, with or without using repc, the Qt Remote Objects Compiler.

​1.创建复制到其他节点的源对象,无论是否使用Qt远程对象编译器repc。

2.Optionally, create the Registry. Otherwise, use direct connections.

​2.创建注册表(可选)。否则,请使用直接连接。

3.Create a host node so that the source object can be shared.

3.创建一个主机节点,以便共享源对象。

4.Call the node's enableRemoting() function to share the source object.

​4.调用节点的enableRemoting()函数以共享源对象。

On the replica side, we need to:

在复制品方面,我们需要:

1.Optionally, use repc to generate a Replica header for your project.

​1.(可选)使用repc为项目生成Replica头文件。

2.Create the node that will connect with the Source host node.

​2.创建将与源主机节点连接的节点。

3.Call the node's acquire() function to create a pointer to a replica.

​3.调用节点的acquire()函数以创建指向副本的指针。

The following examples illustrate both repc-compiled static objects and dynamic source objects. Additionally, they also show direct connections as well as connections that use a Registry between nodes.

​以下示例说明了repc编译的静态对象和动态源对象。此外,它们还显示了直接连接以及节点之间使用注册表的连接。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值