示例 2:与动态副本直接连接
源端无需做任何更改,因为动态副本只影响请求者节点获取副本的方式。因此,我们使用例 1 中所示的源代码。
- 将副本生成添加到项目中。
由于副本是动态获取的,因此与例 1 不同,不需要
.rep
文件。 - 创建远程节点并将其连接到源主机节点。
这一步的代码与例 1 相同。
QRemoteObjectNode repNode; // create remote object node repNode.connectToNode(QUrl(QStringLiteral("local:replica"))); // connect with remote host node
- 获取远程源对象的副本。
在
main.cpp
中,我们使用QSharedPointer 来保存远程对象的副本,然后实例化一个副本请求者对象:#include <QCoreApplication> #include "dynamicclient.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QSharedPointer<QRemoteObjectDynamicReplica> ptr; // shared pointer to hold replica QRemoteObjectNode repNode; repNode.connectToNode(QUrl(QStringLiteral("local:replica"))); ptr.reset(repNode.acquireDynamic("SimpleSwitch")); // acquire replica of source from host node DynamicClient rswitch(ptr); // create client switch object and pass replica reference to it return a.exec(); }
请求者类DynamicClient
的完整声明和定义如下:
dynamicclient.h
#ifndef _DYNAMICCLIENT_H #define _DYNAMICCLIENT_H #include <QObject> #include <QSharedPointer> #include <QRemoteObjectNode> #include <qremoteobjectdynamicreplica.h> class DynamicClient : public QObject { Q_OBJECT public: DynamicClient(QSharedPointer<QRemoteObjectDynamicReplica> ptr); ~DynamicClient() override = default; Q_SIGNALS: void echoSwitchState(bool switchState);// this signal is connected with server_slot(..) slot of source object and echoes back switch state received from source public Q_SLOTS: void recSwitchState_slot(bool); // Slot to receive source state void initConnection_slot(); // Slot to connect signals/slot on replica initialization private: bool clientSwitchState; // holds received server switch state QSharedPointer<QRemoteObjectDynamicReplica> reptr;// holds reference to replica }; #endif
dynamicclient.cpp
#include "dynamicclient.h"// 构造函数DynamicClient::DynamicClient(QSharedPointer<QRemoteObjectDynamicReplica> ptr) : QObject(nullptr),clientSwitchState(false),reptr(ptr) {// 复制的连接信号有效,信号槽初始化已更改 QObject::connect(reptr.data(), &QRemoteObjectDynamicReplicavoid DynamicClient::initConnection_slot() {//Connect source replica signal currStateChanged() with client' s // recSwitchState() slot to receive source's current state: QObject::connect(reptr.data(),SIGNAL(currStateChanged(bool)), this,SLOT(recSwitchState_slot(bool)));// Connect client's echoSwitchState(..) signal with replica's// server_slot(..) to echo back received state: QObject::connect(this,SIGNAL(echoSwitchState(bool)),reptr.data(),SLOT(server_slot(bool))); }voidDynamicClient::recSwitchState_slot(boolvalue) {// 使用副本属性从源获取 "currState":clientSwitchState= reptr->property("currState").toBool(); qDebug() << "Received source state " << value << clientSwitchState; // Q_EMIT echoSwitchState(clientSwitchState); }
与源端示例一起运行时,输出结果与示例 1 相同。
© 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.