示例 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.