QRemoteObjectRegistry类有什么用?可以参考下图:
在某些应用程序中,需要有多个源,而为每个副本分别连接到每个源则显得很冗余,因此QRemoteObjectRegistryHost的任务是为多个源提供一个连接点,并通过它连接每个副本。
QRemoteObjectRegistry类型的registry属性。而它有两个关键信号:
void remoteObjectAdded(const QRemoteObjectSourceLocation &entry);
void remoteObjectRemoved(const QRemoteObjectSourceLocation &entry);
当带有Registry的QtRO网络中有新的Source被enableRemoting出来,则每个接入的Node的registry属性都会发出remoteObjectAdded这个信号;当有Source被disableRemoting之后,每个接入的Node的registry都会发出remoteObjectRemoved信号。
Source 端代码
simpleswitch.h 和 simpleswitch.cpp 不做修改, 不同之处在于主机节点的创建和连接方式:
main.cpp
#include <QCoreApplication>
#include "simpleswitch.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
SimpleSwitch srcSwitch; // create SimpleSwitch
QRemoteObjectRegistryHost regNode(QUrl(QStringLiteral("local:registry"))); // create node that hosts registry
QRemoteObjectHost srcNode(QUrl(QStringLiteral("local:switch")), QUrl(QStringLiteral("local:registry"))); // create node that will host source and connect to registry
//Note, you can add srcSwitch directly to regNode if desired.
//We use two Nodes here, as the regNode could easily be in a third process.
srcNode.enableRemoting(&srcSwitch); // enable remoting of source object
return a.exec();
}
Replica 端代码
本例中使用的请求者对象是示例2中讨论的动态副本客户端。
唯一的修改是主要的。cpp:创建注册表节点以获取副本:
QRemoteObjectNode repNode(QUrl(QStringLiteral("local:registry")));