Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Example 2: Direct Connection with a Dynamic Replica#

Describes how the Qt Remote Objects establishes a direct connection with a dynamic replica. .. _qtro-example2: Initially, a dynamic replica is created as a “bare” QObject - without properties, signals or slots. Then, during initialization, QtRO returns the API for the object, after the connection to the source is made. Thus, the API is added to the object at runtime.

There are no changes to be made on the source side, as a dynamic Replica only impacts how the requestor node acquires the replica. So, we use the source-side code shown in Example 1 .

  1. Add replica generation to the project.

    Because the replica is dynamically acquired, no .rep file is required unlike in Example 1 .

  2. Create the remote node and connect it to the source host node.

    The code for this step is unchanged from Example 1 .

    QRemoteObjectNode repNode # create remote object node
    repNode.connectToNode(QUrl("local:replica")) # connect with remote host node
    
  3. Acquire a replica of the remote source object.

    In main.cpp, we use a QSharedPointer to hold a replica of the remote object, and then instantiate a replica requestor object:

    from PySide6.QtCore import QCoreApplication
    from dynamicclient import *
    if __name__ == "__main__":
    
        a = QCoreApplication(argc, argv)
        QSharedPointer<QRemoteObjectDynamicReplica> ptr # shared pointer to hold replica
        repNode = QRemoteObjectNode()
        repNode.connectToNode(QUrl("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()
    

The complete declaration and definition of the requestor class, DynamicClient, is as follows:

dynamicclient.h

#ifndef _DYNAMICCLIENT_H
#define _DYNAMICCLIENT_H
from PySide6.QtCore import QObject

from PySide6.QtRemoteObjects import QRemoteObjectNode

class DynamicClient(QObject):

    Q_OBJECT
# public
    DynamicClient(QSharedPointer<QRemoteObjectDynamicReplica> ptr)
    ~DynamicClient() override = default
Q_SIGNALS:
    def echoSwitchState(server_slot(..):
public Q_SLOTS:
    def recSwitchState_slot(bool):
    def initConnection_slot():
# private
    bool clientSwitchState # holds received server switch state
    QSharedPointer<QRemoteObjectDynamicReplica> reptr# holds reference to replica

#endif

dynamicclient.cpp

from dynamicclient import *
# constructor
def __init__(self, ptr):
    super().__init__(None)
    self.clientSwitchState = False
    self.reptr = ptr

    #connect signal for replica valid changed with signal slot initialization
    QObject.connect(reptr.data(), QRemoteObjectDynamicReplica.initialized, self,
                     DynamicClient.initConnection_slot)

# Function to initialize connections between slots and signals
def initConnection_slot(self):

    # Connect source replica signal currStateChanged() with client's
    # recSwitchState() slot to receive source's current state:
    QObject.connect(reptr.data(), SIGNAL(currStateChanged(bool)), self,
                     SLOT(recSwitchState_slot(bool)))
    # Connect client's echoSwitchState(..) signal with replica's
    # server_slot(..) to echo back received state:
    QObject.connect(self, SIGNAL(echoSwitchState(bool)), reptr.data(), SLOT(server_slot(bool)))

def recSwitchState_slot(self, value):

    # Use replica property to get "currState" from source:
    clientSwitchState = reptr.property("currState").toBool()
    print("Received source state ", value, clientSwitchState)
    # Emit signal to echo received state back to server:
    Q_EMIT echoSwitchState(clientSwitchState)

When run together with the source-side example, the output is identical to Example 1 .