예 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(), &QRemoteObjectDynamicReplica::initialized, this, &DynamicClient::initConnection_slot); }// 슬롯과 신호 간 연결을 초기화하는 함수void DynamicClient::initConnection_slot() { // 소스 레플리카 신호 currStateChanged()를 클라이언트의 // recSwitchState() 슬롯과 연결하여 소스의 현재 상태를 수신합니다: QObject::connect(reptr.data(), SIGNAL(currStateChanged(bool)), this,SLOT(recSwitchState_slot(bool)); // 클라이언트의 echoSwitchState(..) 신호를 레플리카의 // server_slot(..) 과 연결하여 수신 상태를 다시 에코합니다: QObject::connect(this, SIGNAL(echoSwitchState(bool)), reptr.data(), SLOT(server_slot(bool)); }void DynamicClient::recSwitchState_slot(bool value) { // 복제 프로퍼티를 사용하여 소스에서 "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.