示例 2:与动态副本的直接连接

由于动态 副本 只会影响请求器节点获取副本的方式,因此在源端不需要进行任何更改。因此,我们使用示例 1中显示的源端代码。

  1. 将副本生成添加到项目中。

    由于副本是动态获取的,因此不要求有 .rep 文件,这与示例 1不同。

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

// constructor
DynamicClient::DynamicClient(QSharedPointer<QRemoteObjectDynamicReplica> ptr)
    : QObject(nullptr), clientSwitchState(false), reptr(ptr)
{
    //connect signal for replica valid changed with signal slot initialization
    QObject::connect(reptr.data(), &QRemoteObjectDynamicReplica::initialized, this,
                     &DynamicClient::initConnection_slot);
}

// Function to initialize connections between slots and signals
void DynamicClient::initConnection_slot()
{
    // Connect source replica signal currStateChanged() with client's
    // recSwitchState() slot to receive source's current state:
    QObject::connect(reptr.data(), SIGNAL(currStateChanged(bool)), this,
                     SLOT(recSwitchState_slot(bool)));
    // Connect client's echoSwitchState(..) signal with replica's
    // server_slot(..) to echo back received state:
    QObject::connect(this, SIGNAL(echoSwitchState(bool)), reptr.data(), SLOT(server_slot(bool)));
}

void DynamicClient::recSwitchState_slot(bool value)
{
    // Use replica property to get "currState" from source:
    clientSwitchState = reptr->property("currState").toBool();
    qDebug() << "Received source state " << value << clientSwitchState;
    // Emit signal to echo received state back to server:
    Q_EMIT echoSwitchState(clientSwitchState);
}

当与源端示例一起运行时,输出与示例 1相同。

© 2024 The Qt Company Ltd. 本文件中包含的文档贡献的版权属于其各自所有者。本文件中提供的文档依据GNU 自由文档许可证版本 1.3 的条款进行许可,由自由软件基金会发布。Qt 和对应的徽标是 The Qt Company Ltd. 在芬兰以及其他全球国家的商标。所有其他商标均为其各自所有者的财产。