このページでは

Qt Remote Objects - 外部 QIODevices

外部 QIODevices

Qt Remote Objects は、QTcpServer やQTcpSocket といったペアなど、いくつかの通信チャネルを標準でサポートしています。 tcp 用のQUrl 、あるいは(QLocalServer とQLocalSocket のペア用の)名前が指定されていれば、リスニングや接続に必要なコードは定型的なものであり、Qt によって内部的に処理されます。Qt Remote Objects は他のタイプのQIODevice もサポートしており、QRemoteObjectNode クラスは、カスタムコードが必要なケースに対応するための追加メソッドを提供しています。

以下に、TCP/IP を使用した単純な例を示します。より現実的な例としては SSL 接続が挙げられますが、その場合は証明書の設定などが必要になります。

// Create the server and listen outside of QtRO
QTcpServer tcpServer;
tcpServer.listen(QHostAddress(QStringLiteral("127.0.0.1")), 65213);

// Create the host node.  We don't need a hostUrl unless we want to take
// advantage of external schemas (see next example).
QRemoteObjectHost srcNode;

// Make sure any connections are handed to QtRO
QObject::connect(&tcpServer, &QTcpServer::newConnection, &srcNode,
                 [&srcNode, &tcpServer]() {
    srcNode.addHostSideConnection(tcpServer.nextPendingConnection());
});

レプリカ側のコードでは、ホストへの接続を手動で行う必要があります

QRemoteObjectNode repNode;
QTcpSocket *socket = new QTcpSocket(&repNode);
QObject::connect(socket, &QTcpSocket::connected, &repNode,
        [socket, &repNode]() {
    repNode.addClientSideConnection(socket);
});
socket->connectToHost(QHostAddress(QStringLiteral("127.0.0.1")), 65213);

外部スキーマ

QIODevice の各サイドを作成し、前述のようにQRemoteObjectNode::addClientSideConnection(QIODevice *ioDevice)およびQRemoteObjectHostBase::addHostSideConnection(QIODevice *ioDevice)を呼び出すことが可能です。 これは完全にサポートされていますが、クライアント側が接続の確立方法を知っているか、その情報を取得する手段を持っている必要があります。これこそが、レジストリが解決するために設計された問題そのものです。

Qt Remote Objects また、レジストリで「外部スキーマ」を使用することも可能であり、これは接続の確立に役立ちます。QRemoteObjectHost 側では、ユーザーは希望するスキーマを指定してhostUrlを設定する必要があります。

// Use standard tcp url for the registry
const QUrl registryUrl = QUrl(QStringLiteral("tcp://127.0.0.1:65212"));
// Use "exttcp" for the "external" interface
const QUrl extUrl = QUrl(QStringLiteral("exttcp://127.0.0.1:65213"));

// Create the server and listen outside of QtRO
QTcpServer tcpServer;
tcpServer.listen(QHostAddress(extUrl.host()), extUrl.port());

// We need a registry for everyone to connect to
QRemoteObjectRegistryHost registry(registryUrl);

// Finally, we create our host node and register "exttcp" as our schema.
// We need the AllowExternalRegistration parameter to prevent the node from
// setting a hostUrlInvalid error.
QRemoteObjectHost srcNode(extUrl, registryUrl, QRemoteObjectHost::AllowExternalRegistration);
// From now on, when we call enableRemoting() from this node, the registry
// will be updated to show the Source object at extUrl.

レプリカ側では、QRemoteObjectNode が外部スキーマが検出された際に使用されるコールバックを登録する必要があります。このコールバックはRemoteObjectSchemaHandler でなければなりません。

// Use standard tcp url for the registry
const QUrl registryUrl = QUrl(QStringLiteral("tcp://127.0.0.1:65212"));

// This time create the node connected to the registry
QRemoteObjectNode repNode(registryUrl);

// Create the RemoteObjectSchemaHandler callback
QRemoteObjectNode::RemoteObjectSchemaHandler setupTcp = [&repNode](QUrl url) {
    QTcpSocket *socket = new QTcpSocket(&repNode);
    connect(socket, &QTcpSocket::connected,
            [socket, &repNode]() {
        repNode.addClientSideConnection(socket);
    });
    connect(socket, &QSslSocket::errorOccurred,
            [socket](QAbstractSocket::SocketError error) {
        delete socket;
    });
    socket->connectToHost(url.host(), url.port());
};

// Once we call registerExternalSchema, the above method will be called
// whenever the registry sees an object we are interested in on "exttcp"
repNode.registerExternalSchema(QStringLiteral("exttcp"), setupTcp);

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