Qt Remote Objects - Périphériques QIODevices externes
QIODevices externes
Qt Remote Objects prend en charge plusieurs canaux de communication dès l’installation, tels que la paire QTcpServer et QTcpSocket. Une fois l’ QUrl e souhaitée pour TCP ou le nom souhaité (pour la paire QLocalServer et QLocalSocket ) spécifiée, le code nécessaire à l’écoute et à la connexion est standard et géré en interne par Qt. Qt Remote Objects prend également en charge d’autres types d’ QIODevice, et les classes QRemoteObjectNode fournissent des méthodes supplémentaires pour les cas où un code personnalisé est nécessaire.
Un exemple simplifié avec TCP/IP est présenté ci-dessous. Un exemple plus réaliste utiliserait une connexion SSL, ce qui nécessiterait la configuration de certificats, etc.
// 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());
});Le code côté réplique doit se connecter manuellement à l’hôte
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);Schémas externes
Il est possible de créer chaque côté de l’ QIODevice et d’appeler QRemoteObjectNode::addClientSideConnection(QIODevice *ioDevice) et QRemoteObjectHostBase::addHostSideConnection(QIODevice *ioDevice) comme indiqué ci-dessus. Cette fonctionnalité est entièrement prise en charge, mais nécessite que le client sache comment établir la connexion ou dispose d’un moyen de découvrir ces informations. C’est précisément le problème que le registre a été conçu pour résoudre.
Qt Remote Objects Il permet également d’utiliser des « schémas externes » avec le registre, ce qui facilite l’établissement de la connexion. Du côté de l’ QRemoteObjectHost, l’utilisateur doit définir la propriété hostUrl avec le schéma souhaité.
// 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.Du côté de la réplique, l’ QRemoteObjectNode doit enregistrer un callback à utiliser lorsque le schéma externe est détecté. Le callback doit être de type 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.