例 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

#include "dynamicclient.h"

// コンストラクタ
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と同じになります。

© 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.