Qt远程对象

Qt Remote Objects

Qt远程对象

Remote Object Concepts

远程对象概念

Qt Remote Objects (QtRO) is an Inter-Process Communication (IPC) module developed for Qt. This module extends Qt's existing functionalities to enable information exchange between processes or computers, easily.

Qt远程对象(QtRO)是为Qt开发的进程间通信(IPC)模块。该模块扩展了Qt的现有功能,使进程或计算机之间能够轻松交换信息。

One of Qt's key features, to enable this information exchange, is the distinction between an object's API (defined by its properties, signals, and slots) and the implementation of that API. QtRO's purpose is to meet that expected API, even if the true QObject is in a different process. A slot called on a copy of an object (the Replica in QtRO) is forwarded to the true object (the Source in QtRO) for handling. Every Replica receives updates to the Source, either property changes or emitted signals.

​实现这种信息交换的Qt的关键特性之一是对象的API(由其属性、信号和槽定义)与API的实现之间的区别。QtRO的目的是满足预期的API,即使真正的QObject处于不同的进程中。对象副本(QtRO中的Replica)上调用的插槽被转发给真正的对象(QtRO中的Source)进行处理。每个副本都会接收源的更新,无论是属性更改还是发出的信号。

Replica is a light-weight proxy for the Source object, but a Replica supports the same connections and behavior of QObjects, which makes it usable in the same way as any other QObject that Qt provides. Behind the scenes, QtRO handles everything that's necessary for the Replica to look like its Source.

​Replica是Source对象的轻量级代理,但Replica支持与QObjects相同的连接和行为,这使得它可以像Qt提供的任何其他QObject一样使用。在幕后,QtRO处理副本看起来像源代码所需的一切。

Note that Remote Objects behave differently from traditional Remote Procedure Call (RPC) implementations, for example:

请注意,远程对象的行为与传统的远程过程调用(RPC)实现不同,例如:

  • In RPC, the client makes a request and waits for the response.
  • 在RPC中,客户端发出请求并等待响应。
  • In RPC, the server doesn't push anything to the client unless it's in response to a request.
  • 在RPC中,服务器不会向客户端推送任何内容,除非它是对请求的响应。
  • Often, the design of RPC is such that different clients are independent of each other: for instance, two clients can ask a mapping service for directions and get different results.
  • 通常,RPC的设计使得不同的客户端彼此独立:例如,两个客户端可以向映射服务请求方向,并得到不同的结果。

While it is possible to implement this RPC-style behavior in QtRO, as Sources without properties, and slots that have return values, QtRO hides the fact that the processing is really remote. You let a node give you the Replica instead of creating it yourself, possibly use the status signals (isReplicaValid()), but then interact with the object like you would with any other QObject-based type.

​虽然可以在QtRO中实现这种RPC风格的行为,但作为没有属性的源和有返回值的槽,QtRO隐藏了处理实际上是远程的这一事实。可以让节点提供副本,而不是自己创建副本,可能会使用状态信号(isReplicaValid()),但然后与对象交互,就像与任何其他基于QObject的类型交互一样。

Use Case: GPS

用例:GPS

Consider a sensor such as a Global Positioning System (GPS) receiver. In QtRO terms:

考虑一个传感器,如全球定位系统(GPS)接收器。用QtRO术语来说:

  • The Source would be the process that directly interacts with the GPS hardware and derives your current location.
  • ​Source是直接与GPS硬件交互并得出当前位置的过程。
  • The location would be exposed as QObject properties; the periodic updates to the location would update these properties and emit property changed signals.
  • ​该位置将作为QObject属性公开;对位置的定期更新将更新这些属性并发出属性更改信号。
  • Replicas would be created in other processes and would always know your current location, but wouldn't need any of the logic to compute the location from the sensor data.
  • 副本将在其他进程中创建,并且始终知道您的当前位置,但不需要任何逻辑来根据传感器数据计算位置。
  • Connecting to the location changed signal on the Replica would work as expected: the signal emitted from the Source would trigger the signal emission on every Replica.
  • 连接到副本上的位置更改信号将按预期工作:源发出的信号将触发每个副本上的信号发射。

Use Case: Printer Access

用例:打印机访问

Consider a service that provides access to a printer. In QtRO terms:

考虑提供打印机访问的服务。用QtRO术语来说:

  • The Source would be the process controlling the printer directly.
  • ​源将是直接控制打印机的进程。
  • The ink levels and printer status would be monitored by QObject properties. Updates to these properties would emit property changed signals.
  • ​墨水量和打印机状态将由QObject属性监控。对这些属性的更新将发出属性更改信号。
  • The key feature – being able to print something – needs to be passed back to the printer. Incidentally, this aligns with the Qt slot mechanism, which QtRO uses as the way for Replicas to make calls on the Source. In effect, properties and signals go from Source to Replicas; slots go from Replica to Source.
  • ​关键功能——能够打印东西——需要传递回打印机。顺便说一句,这与Qt槽机制是一致的,QtRO使用Qt槽机制作为Replicas对Source进行调用的方式。实际上,属性和信号从源传递到副本;插槽从副本转到源。
  • When a print request is accepted, the printer status would change, triggering a change in the status property. This would then be reported to all Replicas.
  • 当接受打印请求时,打印机状态将更改,从而触发状态属性的更改。然后,这将报告给所有副本。

Using the Module

使用模块

Using a Qt module requires linking against the module library, either directly or through other dependencies. Several build tools have dedicated support for this, including CMake and qmake.

​使用Qt模块需要直接或通过其他依赖关系链接到模块库。一些构建工具对此有专门的支持,包括CMake和qmake。

Building with CMake

使用CMake构建

Use the find_package() command to locate the needed module components in the Qt6 package:

使用find_package()命令在Qt6包中查找所需的模块组件:

find_package(Qt6 REQUIRED COMPONENTS RemoteObjects)

See also the Build with CMake overview.

​另请参阅使用CMake构建概述。

Building with qmake

使用qmake构建

To configure the module for building with qmake, add the module as a value of the QT variable in the project's .pro file:

要配置使用qmake构建的模块,请将该模块作为QT变量的值添加到项目的.pro文件中:

QT       += remoteobjects

Articles and Guides

文章和指南

API Reference

Licenses

许可

Qt Remote Objects is available under commercial licenses from The Qt Company. In addition, it is available under the GNU Lesser General Public License, version 3, or the GNU General Public License, version 2. See Qt Licensing for further details.

​Qt远程对象可从Qt公司获得商业许可。此外,它还可以在GNU较宽松通用公共许可证第3版或GNU通用公共许可证第2版下使用。有关更多详细信息,请参阅Qt许可。

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值