源代码: void QRemoteObjectSourceIo::onServerDisconnect(QObject *conn) { IoDeviceBase *connection = qobject_cast(conn); m_connections.remove(connection); qRODebug(this) << "OnServerDisconnect"; for (QRemoteObjectRootSource *root : qAsConst(m_sourceRoots)) root->removeListener(connection); const QUrl location = m_registryMapping.value(connection); emit serverRemoved(location); m_registryMapping.remove(connection); connection->close(); connection->deleteLater(); } bool QRemoteObjectHostBase::setHostUrl(const QUrl &hostAddress) { Q_D(QRemoteObjectHostBase); if (d->remoteObjectIo) { d->setLastError(ServerAlreadyCreated); return false; } d->remoteObjectIo = new QRemoteObjectSourceIo(hostAddress, this); if (d->remoteObjectIo->m_server.isNull()) { //Invalid url/scheme d->setLastError(HostUrlInvalid); delete d->remoteObjectIo; d->remoteObjectIo = 0; return false; } //If we've given a name to the node, set it on the sourceIo as well if (!objectName().isEmpty()) d->remoteObjectIo->setObjectName(objectName()); //Since we don't know whether setHostUrl or setRegistryUrl/setRegistryHost will be called first, //break it into two pieces. setHostUrl connects the RemoteObjectSourceIo->[add/remove]RemoteObjectSource to QRemoteObjectReplicaNode->[add/remove]RemoteObjectSource //setRegistry* calls appropriately connect RemoteObjecSourcetIo->[add/remove]RemoteObjectSource to the registry when it is created QObject::connect(d->remoteObjectIo, &QRemoteObjectSourceIo::remoteObjectAdded, this, &QRemoteObjectHostBase::remoteObjectAdded); QObject::connect(d->remoteObjectIo, &QRemoteObjectSourceIo::remoteObjectRemoved, this, &QRemoteObjectHostBase::remoteObjectRemoved); return true; } 建议修改代码: void QRemoteObjectSourceIo::onServerDisconnect(QObject *conn) { ServerIoDevice *connection = qobject_cast(conn); m_connections.remove(connection); qRODebug(this) << "OnServerDisconnect" << connection->objectName(); Q_FOREACH (QRemoteObjectRootSource *root, m_sourceRoots) { root->removeListener(connection); if (root->name() == connection->objectName()) emit remoteObjectDisconnect(root->name(), root->m_api->typeName()); } const QUrl location = m_registryMapping.value(connection); emit serverRemoved(location); m_registryMapping.remove(connection); connection->close(); connection->deleteLater(); } bool QRemoteObjectHostBase::setHostUrl(const QUrl &hostAddress) { Q_D(QRemoteObjectHostBase); if (d->remoteObjectIo) { d->setLastError(ServerAlreadyCreated); return false; } d->remoteObjectIo = new QRemoteObjectSourceIo(hostAddress, this); if (d->remoteObjectIo->m_server.isNull()) { //Invalid url/scheme d->setLastError(HostUrlInvalid); delete d->remoteObjectIo; d->remoteObjectIo = 0; return false; } //If we've given a name to the node, set it on the sourceIo as well if (!objectName().isEmpty()) d->remoteObjectIo->setObjectName(objectName()); QObject::connect(d->remoteObjectIo, &QRemoteObjectSourceIo::remoteObjectDisconnect, this, &QRemoteObjectHostBase::remoteObjectDisconnect); //Since we don't know whether setHostUrl or setRegistryUrl/setRegistryHost will be called first, //break it into two pieces. setHostUrl connects the RemoteObjectSourceIo->[add/remove]RemoteObjectSource to QRemoteObjectReplicaNode->[add/remove]RemoteObjectSource //setRegistry* calls appropriately connect RemoteObjecSourcetIo->[add/remove]RemoteObjectSource to the registry when it is created QObject::connect(d->remoteObjectIo, &QRemoteObjectSourceIo::remoteObjectAdded, this, &QRemoteObjectHostBase::remoteObjectAdded); QObject::connect(d->remoteObjectIo, &QRemoteObjectSourceIo::remoteObjectRemoved, this, &QRemoteObjectHostBase::remoteObjectRemoved); return true; }