QRemoteObjectHostBase Class
QRemoteObjectHostBase 类提供了Host 和RegistryHost 类共有的基础功能。更多内容...
| 头文件: | #include <QRemoteObjectHostBase> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS RemoteObjects) target_link_libraries(mytarget PRIVATE Qt6::RemoteObjects) |
| qmake: | QT += remoteobjects |
| 继承自: | QRemoteObjectNode |
| 继承自: |
公共类型
| enum | AllowedSchemas { BuiltInSchemasOnly, AllowExternalRegistration } |
公共函数
| void | addHostSideConnection(QIODevice *ioDevice) |
| bool | disableRemoting(QObject *remoteObject) |
| bool | enableRemoting(ObjectType *object) |
| bool | enableRemoting(QObject *object, const QString &name = QString()) |
| bool | enableRemoting(QAbstractItemModel *model, const QString &name, const QList<int> roles, QItemSelectionModel *selectionModel = nullptr) |
| bool | proxy(const QUrl ®istryUrl, const QUrl &hostUrl = {}, QRemoteObjectHostBase::RemoteObjectNameFilter filter = [](QStringView, QStringView) {return true; }) |
| bool | reverseProxy(QRemoteObjectHostBase::RemoteObjectNameFilter filter = [](QStringView, QStringView) {return true; }) |
重新实现的公共函数
| virtual void | setName(const QString &name) override |
详细说明
QRemoteObjectHostBase 是一个无法直接实例化的基类。它提供了所有主机节点(Host 和RegistryHost )共有的enableRemoting 和disableRemoting 功能,以及在远程对象网络上暴露源对象所需的逻辑。
成员类型文档
enum QRemoteObjectHostBase::AllowedSchemas
此枚举用于指定节点是否接受 hostUrl 具有未识别方案的 URL。默认情况下,仅接受具有已知方案的 URL,但使用AllowExternalRegistration 将使注册表能够将您的外部(对 QtRO 而言)URL 传递给客户端节点。
| 常量 | 值 | 描述 |
|---|---|---|
QRemoteObjectHostBase::BuiltInSchemasOnly | 0 | 仅允许将 hostUrl 设置为 QtRO 支持的模式。这是默认值,若提供未识别的模式,将触发节点错误。 |
QRemoteObjectHostBase::AllowExternalRegistration | 1 | 提供的方案将作为外部方案进行注册 |
另请参阅 QRemoteObjectHost 。
成员函数文档
void QRemoteObjectHostBase::addHostSideConnection(QIODevice *ioDevice)
为了通过外部QIODevices将 源对象 QRemoteObjectHost::enableRemoting(),Qt Remote Objects 需要访问相应节点之间的通信通道(即QIODevice )。在源端,通过调用addHostSideConnection()并传入ioDevice 作为输入,即可实现这一功能。 即使不调用 addHostSideConnection,任何enableRemoting() 调用仍可正常工作,但若未提供与副本节点(Replica)的连接,该节点将无法共享源对象。在调用此函数之前,必须先调用setHostUrl(),并传入一个唯一的 URL 以及AllowExternalRegistration 。
另请参阅 addClientSideConnection 。
[invokable] bool QRemoteObjectHostBase::disableRemoting(QObject *remoteObject)
禁用QObject 的远程访问remoteObject 。如果当前节点是客户端节点,或者remoteObject 未注册,则返回false ;如果成功禁用了Source对象的远程访问,则返回true 。
警告: 调用此方法后,该对象的副本 将不再有效。
注意: 可以通过元对象系统和 QML 调用此 函数。参见Q_INVOKABLE 。
另请参阅 enableRemoting()。
template <template <typename> typename ApiDefinition, typename ObjectType> bool QRemoteObjectHostBase::enableRemoting(ObjectType *object)
此模板函数重载使主节点能够通过一个具有指定(且在编译时经过检查)接口的源(QObject )object 提供远程访问。连接到托管该对象的主节点的客户端节点可以获取该源的副本。
以下示例对此进行了最佳说明:
#include "rep_TimeModel_source.h"
MinuteTimer timer;
hostNode.enableRemoting<MinuteTimerSourceAPI>(&timer);此处,MinuteTimerSourceAPI 即为源(ApiDefinition ),代表由 TimeModel.rep 文件定义的一组信号/槽/属性。系统会进行编译时检查,以验证输入的源(QObject )能否暴露所请求的 API;否则将导致编译失败。 这允许暴露object 接口的子集,并支持信号/插槽连接所支持的转换类型。
如果当前节点是客户端节点,或者QObject 已注册为远程节点,则返回false ;如果QObject 的远程功能已成功启用,则返回true 。
另请参阅 disableRemoting()。
[invokable] bool QRemoteObjectHostBase::enableRemoting(QObject *object, const QString &name = QString())
允许主机节点动态提供对QObject object 的远程访问。连接到托管此对象的节点的客户端节点可以获取该源的副本。
可选的name 定义了可通过QRemoteObjectNode::acquire() 获取QObject 的查找名称。若未显式设置,则使用 QCLASSINFO_REMOTEOBJECT_TYPE 中给定的名称。若未为QObject 定义此类宏,则使用QObject::objectName()。
如果当前节点是客户端节点,或者QObject 已注册为远程对象,则返回false ;如果动态QObject 的远程功能已成功启用,则返回true 。
注意: 可通过元对象系统和 QML 调用此 函数。参见Q_INVOKABLE 。
另请参阅 disableRemoting()。
bool QRemoteObjectHostBase::enableRemoting(QAbstractItemModel *model, const QString &name, const QList<int> roles, QItemSelectionModel *selectionModel = nullptr)
enableRemoting() 的此重载专用于QAbstractItemModel 类型(或任何从QAbstractItemModel 派生的类型)。如果希望将模型及其 HMI 放置在不同的进程中,此功能将非常有用。
三个必填参数分别是:model 本身、用于查找模型的name ,以及应在副本(Replica)端公开的roles 。若需在源(Source)和副本(Replica)之间同步选择状态,可使用可选参数selectionModel 。仅在使用单个副本时才建议使用此参数。
在后台,Qt Remote Objects 会批量处理 data() 查询,并在可能的情况下预取数据,以使模型交互尽可能响应迅速。
如果当前节点是客户端节点,或者QObject 已注册为远程节点,则返回false ;如果QAbstractItemModel 的远程功能已成功启用,则返回true 。
另请参阅 disableRemoting()。
bool QRemoteObjectHostBase::proxy(const QUrl ®istryUrl, const QUrl &hostUrl = {}, QRemoteObjectHostBase::RemoteObjectNameFilter filter = [](QStringView, QStringView) {return true; })
从其他网络转发远程对象
当您希望在多个网络间共享源对象时,代理功能非常有用。例如,如果您有一个使用“仅目标”连接(如本地连接)的嵌入式目标,并且希望将其中一些相同的对象对外提供访问。
举个具体的例子:假设您有一组进程在目标硬件上通过注册表进行通信,注册表位于“local:registry”,而其他独立进程则通过位于“local:MyHost”的节点访问源对象。 如果您希望通过 TCP 访问这些对象,可以按如下方式创建一个新的 proxyNode:
// myInternalHost is a node only visible on the device...
QRemoteObjectHost myInternalHost("local:MyHost");
myInternalHost.enableRemoting<SomeObject>(&someObject);
// Regular host node, listening on port 12123, so visible to other
// devices
QRemoteObjectHost proxyNode("tcp://localhost:12123");
// Enable proxying objects from nodes on the local machine's internal
// QtRO bus
proxyNode.proxy("local:registry");然后在另一台设备上创建另一个节点:
// NB: localhost resolves to a different ip address than proxyNode
QRemoteObjectHost nodeOnRemoteDevice("tcp://localhost:23234");
// Connect to the target's proxyNode directly, or use a tcp registry...
nodeOnRemoteDevice.connectToNode("tcp://<target device>:12123");
// Because of the proxy, we can get the object over tcp/ip port 12123,
// even though we can't connect directly to "local:MyHost"
SomeObject *so = nodeOnRemoteDevice.acquire<SomeObject>();这将(在内部)在 `proxyNode` 中创建一个节点,该节点会(同样在内部/自动地)连接到指定的注册表(由 `registryUrl ` 参数提供,本例中为 “local:registry”)。 每当 local:registry 发出remoteObjectAdded 信号时,QRemoteObjectSourceLocation 就会被传递给代理调用中指定的filter 。如果该方法返回 true(默认过滤器在不进行任何过滤的情况下直接返回 true),则从内部节点获取该对象,并在 proxyNode 上调用enableRemoting()(待副本初始化完成后)。
如果提供了hostUrl (这是启用reverseProxy 的必要条件,但其他情况下不需要),则内部节点将是一个配置了所提供地址的QRemoteObjectHost 节点。如果未提供hostUrl ,则内部节点将是一个QRemoteObjectNode (而非 HostNode)。
如果该对象是从内部节点获取的,则返回true 。
另请参阅 reverseProxy()。
bool QRemoteObjectHostBase::reverseProxy(QRemoteObjectHostBase::RemoteObjectNameFilter filter = [](QStringView, QStringView) {return true; })
将远程对象转发到另一个网络。
reverseProxy()函数允许扩展proxy()的功能,实质上是在“反向”方向上镜像代理功能。这两者有所不同,因为节点通信并非对称的:一侧使用Source对象调用enableRemoting(),另一侧则调用acquire()来获取Replica。 使用proxy() 允许您通过 acquire 远程“观察”目标设备上的对象,但无法从设备的 local:* 网络中获取目标设备之外的Source对象。这就是 reverseProxy() 的用武之地。如果像这样创建一个 proxyNode:
// myInternalHost is a node only visible on the device...
QRemoteObjectHost myInternalHost("local:MyHost", "local:registry");
// RegistryHost node, listening on port 12123, so visible to other
// devices. The node must be a RegistryHost, so the Sources on
// the "outside" network can be forwarded to the inner network.
QRemoteObjectRegistryHost proxyNode("tcp://0.0.0.0:12123");
// Enable proxying objects from nodes on the local machine's internal
// QtRO bus. Note the hostUrl parameter is now needed.
proxyNode.proxy("local:registry", "local:fromProxy");
proxyNode.reverseProxy();然后在另一台设备上创建另一个节点:
// Listen on a local port, and connect to "proxyNode" as the registry.
// NB: localhost resolves to a different ip address than proxyNode
QRemoteObjectHost nodeOnRemoteDevice("tcp://localhost:23234",
"tcp://<target device>:12123");
// Because of the reverseProxy, we can expose objects on this device
// and they will make their way to proxyNode...
nodeOnRemoteDevice.enableRemoting<OtherObject>(&otherObject);// Acquire() can now see the objects on other devices through proxyNode,
// due to the reverseProxy call.
OtherObject *oo = myInternalHost.acquire<OtherObject>();虽然 `proxy()` 功能允许获取位于其他网络上的源对象,但 `reverseProxy()` 则允许将源对象“推送”到原本无法访问的网络中。
注意: 必须在调用 reverseProxy() 之前先调用proxy(),并且需要向proxy 提供一个 hostUrl,才能使 reverseProxy() 正常工作。reverseProxy() 方法允许应用一个独立的filter 。这个专用于 reverseProxy 的过滤器将接收proxyNode 上新Source对象的通知,并会在它们通过filter 后,在内部节点上获取这些对象。
成功时返回true ,否则返回false 。
注意:目前 仅支持对QRemoteObjectRegistryHost 启用反向代理功能。若在QRemoteObjectHost 节点上调用此方法,将始终返回false 。
另请参阅 proxy()。
[override virtual] void QRemoteObjectHostBase::setName(const QString &name)
重写了:QRemoteObjectNode::setName(const QString &name)。
与 `QObject::setObjectName()`(此方法会调用该方法)类似,但此版本还会将 `name ` 应用于内部类,这些内部类在某些调试输出中会被用到。
© 2026 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.