QRemoteObjectHostBase Class

QRemoteObjectHostBase 类提供HostRegistryHost 类共有的基本功能。更多

头文件: #include <QRemoteObjectHostBase>
CMake: find_package(Qt6 REQUIRED COMPONENTS RemoteObjects)
target_link_libraries(mytarget PRIVATE Qt6::RemoteObjects)
qmake: QT += remoteobjects
继承: QRemoteObjectNode
继承于

QRemoteObjectHostQRemoteObjectRegistryHost

公共类型

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 &registryUrl, 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 是一个不能直接实例化的基类。它提供所有主节点(HostRegistryHost )共享的enableRemotingdisableRemoting 功能,以及在远程对象网络上公开对象所需的逻辑。

成员类型文档

enum QRemoteObjectHostBase::AllowedSchemas

该枚举用于指定节点是否接受 hostUrl 模式未识别的 url。默认情况下,只接受已知模式的 url,但使用AllowExternalRegistration 可以让注册表将外部(QtRO)url 传递给客户端节点。

常量说明
QRemoteObjectHostBase::BuiltInSchemasOnly0只允许将 hostUrl 设置为 QtRO 支持的模式。这是默认值,如果提供了未识别的模式,则会导致 Node 出错。
QRemoteObjectHostBase::AllowExternalRegistration1提供的模式将注册为外部模式

另请参阅 QRemoteObjectHost

成员函数文档

void QRemoteObjectHostBase::addHostSideConnection(QIODevice *ioDevice)

为了通过外部 QIODevices QRemoteObjectHost::enableRemoting()对象,Qt Remote Objects 需要访问各节点之间的通信通道(QIODevice )。addHostSideConnection()调用将ioDevice 作为输入,在源端实现了这一功能。在不调用 addHostSideConnection 的情况下,任何enableRemoting() 调用仍可正常工作,但如果不提供与复制节点的连接,节点将无法共享对象。在调用此函数之前,您必须使用唯一的 URL 和AllowExternalRegistration 调用setHostUrl() 。

另请参见 addClientSideConnection

[invokable] bool QRemoteObjectHostBase::disableRemoting(QObject *remoteObject)

禁用QObject remoteObject 的远程访问。如果当前节点是客户端节点或remoteObject 未注册,则返回false ;如果成功禁用了源对象的远程访问,则返回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 是由 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 派生的类型)。如果您想在不同的进程中使用模型和模型的人机界面,这将非常有用。

所需的三个参数是:model 本身、用于查找模型的name 以及应在副本侧公开的roles 。如果要在副本之间同步选择,可以使用可选的selectionModel 参数。只有在使用单个副本时才建议使用。

在幕后,Qt Remote Objects 会对 data() 查找进行批处理,并在可能的情况下预取数据,以尽可能提高模型交互的响应速度。

如果当前节点是客户端节点,或QObject 已注册要进行远程操作,则返回false ;如果QAbstractItemModel 已成功启用远程操作,则返回true

另请参阅 disableRemoting() 。

bool QRemoteObjectHostBase::proxy(const QUrl &registryUrl, 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() 功能,实际上是 "反向 "镜像代理功能。这些功能是不同的,因为节点通信不是对称的,一方调用enableRemoting() 获取对象,另一方调用acquire() 获取副本。使用proxy() 可以通过获取远程 "观察 "目标设备上的对象,但不允许从设备的本地:* 网络获取非目标对象。这就是 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() 则允许将源对象 "推送 "到无法访问的网络。

注意: proxy() 需要在 reverseProxy() 之前调用,而且需要向proxy 提供 hostUrl,reverseProxy() 才能工作。reverseProxy() 方法允许应用单独的filter 。该反向代理专用过滤器将在proxyNode 上接收新源对象的通知,并在通过filter 的情况下在内部节点上获取这些对象。

成功时返回true ,否则返回false

注: 目前反向代理功能仅支持QRemoteObjectRegistryHost 。在QRemoteObjectHost 节点上调用此方法将始终返回false

另请参阅 proxy()。

[override virtual] void QRemoteObjectHostBase::setName(const QString &name)

重实现:QRemoteObjectNode::setName(const QString &name).

类似于QObject::setObjectName() (此方法调用此方法),但此版本也将name 应用于内部类,在某些调试输出中会用到。

© 2025 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.