Esempio 2: Connessione diretta con una replica dinamica
Non è necessario apportare alcuna modifica sul lato sorgente, poiché una replica dinamica influisce solo sul modo in cui il nodo richiedente acquisisce la replica. Pertanto, utilizziamo il codice lato sorgente mostrato nell’Esempio 1.
- Aggiungere la generazione della replica al progetto.
Poiché la replica viene acquisita dinamicamente, a differenza dell’Esempio 1 non è richiesto alcun file `
.rep`. - Creare il nodo remoto e collegarlo al nodo host di origine.
Il codice per questo passaggio rimane invariato rispetto all'Esempio 1.
QRemoteObjectNode repNode; // create remote object node repNode.connectToNode(QUrl(QStringLiteral("local:replica"))); // connect with remote host node - Acquisire una replica dell’oggetto sorgente remoto.
In
main.cpputilizziamo un oggetto ` QSharedPointer ` per contenere una replica dell'oggetto remoto, quindi istanziamo un oggetto `replica requestor`:#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(); }
La dichiarazione e la definizione complete della classe `requestor`, ` DynamicClient`, sono le seguenti:
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
};
#endifdynamicclient.cpp
#include "dynamicclient.h"
// costruttore
DynamicClient::DynamicClient(QSharedPointer<QRemoteObjectDynamicReplica> ptr)
: QObject(nullptr), clientSwitchState(false), reptr(ptr)
{
// Segnale di connessione per il cambiamento di stato della replica, modificato con l'inizializzazione dello slot del segnale
QObject::connect(reptr.data(), &QRemoteObjectDynamicReplica::initialized, this,
&DynamicClient::initConnection_slot);
}
// Funzione per inizializzare le connessioni tra slot e segnali
void DynamicClient::initConnection_slot()
{
// Collega il segnale della replica sorgente currStateChanged() con lo slot
// recSwitchState() del client per ricevere lo stato attuale della sorgente:
QObject::connect(reptr.data(), SIGNAL(currStateChanged(bool)), this,
SLOT(recSwitchState_slot(bool)));
// Collega il segnale `echoSwitchState(..)` del client conlo
// `server_slot(..)` della replica per restituire lo stato ricevuto:
QObject::connect(this, SIGNAL(echoSwitchState(bool)), reptr.data(), SLOT(server_slot(bool)));
}
void DynamicClient::recSwitchState_slot(bool value)
{
// Utilizza la proprietà della replica per ottenere "currState" dalla sorgente:
clientSwitchState = reptr->property("currState").toBool();
qDebug() << "Received source state " << value << clientSwitchState;
// Emette un segnale per restituire al server lo stato ricevuto:
Q_EMIT echoSwitchState(clientSwitchState);
}Se eseguito insieme all’esempio sul lato sorgente, l’output è identico a quello dell’Esempio 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.