// Create a socket object QTcpSocket *socket = new QTcpSocket(this); // Connect to the host:port socket->connectToHost("example.com", 80); // Wait for connection to establish if (socket->waitForConnected()) { // Disconnect the socket from the host socket->disconnectFromHost(); }
// Slot function to disconnect socket and update UI void onDisconnectButtonClicked() { if (socket->state() == QAbstractSocket::ConnectedState) { socket->disconnectFromHost(); ui->statusLabel->setText("Disconnected"); } } // Connect the disconnect button to the slot function connect(ui->disconnectButton, SIGNAL(clicked()), this, SLOT(onDisconnectButtonClicked()));This example shows how to update the GUI when the socket is disconnected. A slot function is created that checks the current state of the socket and disconnects it if it is connected. The status label is updated to display the new state. Package library: QT (Qt Network module)