例 2:ダイナミックレプリカとの直接接続

動的レプリカはリクエスト元ノードがレプリカを取得する方法にのみ影響するので、ソース側で行う変更はありません。そのため、1で示したソース側のコードを使用します。

  1. レプリカ生成をプロジェクトに追加します。

    レプリカは動的に取得されるので、例1とは異なり、.rep ファイルは必要ありません。

  2. リモート・ノードを作成し、ソース・ホスト・ノードに接続します。

    このステップのコードは1と変わりません。

    QRemoteObjectNode repNode; // create remote object node
    repNode.connectToNode(QUrl(QStringLiteral("local:replica"))); // connect with remote host node
  3. リモート・ソース・オブジェクトのレプリカを取得します。

    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

#コンストラクタDynamicClient::DynamicClient(QSharedPointer<QRemoteObjectDynamicReplica>ptr) : QObject(nullptr),clientSwitchState(false),reptr(ptr) {//シグナルスロットの初期化で変更された有効なレプリカのコネクトシグナル   QObject::connect(reptr.data())、&。QRemoteObjectDynamicReplica::initialized, this, &DynamicClient::initConnection_slot); }// スロットとシグナル間の接続を初期化する関数voidDynamicClient::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))); }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.