예 3: 레지스트리를 사용하여 원격 노드에 연결하기

소스 설정

예제의 simpleswitch.hsimpleswitch.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: 레플리카를 얻기 위해 레지스트리 노드가 생성된다는 점입니다:

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

소스 측 예제와 함께 실행하면 출력은 예제 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.