示例 3:使用注册表连接远程节点
设置源
示例中的 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
// Create node that hosts registry:
QRemoteObjectRegistryHost regNode(QUrl(QStringLiteral("local:registry")));
// Create node that will host source and connect to registry:
QRemoteObjectHost srcNode(QUrl(QStringLiteral("local:replica")),
QUrl(QStringLiteral("local: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();
}设置副本
本示例中使用的请求者对象是示例 2 中讨论的动态副本客户端。
唯一的修改在于main.cpp :创建了一个Registry节点来获取一个Replica:
QRemoteObjectNode repNode(QUrl(QStringLiteral("local:registry")));与源端示例一起运行时,输出结果与示例 1 完全相同。
© 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.