范例 3:使用注册表连接到远程节点

设置 Source (源)

The simpleswitch.h and simpleswitch.cpp 源来自 范例 can be used without modification. The difference is in the way a host node is created and connected to the registry:

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 (复本)

The requestor object used for this example is the dynamic replica client discussed in 范例 2 .

The only modification is in main.cpp 注册 node is created to acquire a 复本 :

    QRemoteObjectNode repNode(QUrl(QStringLiteral("local:registry")));
					

When run together with the source-side example, the output is identical to 范例 1 .