Nesta página

Exemplo 3: Conexões com nós remotos usando um registro

Configurar a fonte

As fontes simpleswitch.h e simpleswitch.cpp do Exemplo podem ser usadas sem modificações. A diferença está na maneira como um nó host é criado e conectado ao registro:

main.cpp

#include <QCoreApplication>
#include "simpleswitch.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    SimpleSwitch srcSwitch; // create SimpleSwitch

    // Create node that hosts registry:
    QRemoteObjectRegistryHost regNode(QUrl(QStringLiteral("local:registry")));

    // Create node that will host source and connect to registry:
    QRemoteObjectHost srcNode(QUrl(QStringLiteral("local:replica")),
                              QUrl(QStringLiteral("local:registry")));
    // Note, you can add srcSwitch directly to regNode if desired.
    // We use two Nodes here, as the regNode could easily be in a third process.

    srcNode.enableRemoting(&srcSwitch); // enable remoting of source object

    return a.exec();
}

Configurando a réplica

O objeto solicitante usado neste exemplo é o cliente de réplica dinâmica discutido no Exemplo 2.

A única modificação está em main.cpp: um nó de Registro é criado para adquirir uma réplica:

    QRemoteObjectNode repNode(QUrl(QStringLiteral("local:registry")));

Quando executado em conjunto com o exemplo do lado da fonte, a saída é idêntica à do Exemplo 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.