When a client terminates the connection, we have to delete the socket from the local m_clients list. The socket's disconnected() signal is already connected to the removeConnection() slot, so we just need to implement it as follows:
void TcpServer::removeConnection() { QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender()); if (!socket) { return; } ui->log->appendPlainText(tr("* Connection removed: %1, port %2\n") .arg(socket->peerAddress().toString()) .arg(socket->peerPort())); m_clients.removeOne(socket); m_receivedData.remove(socket); socket->deleteLater(); ui->disconnectClients->setEnabled(!m_clients.isEmpty()); }