void MainWindow::on_pushConnect_clicked() { qDebug() << "点击连接:" << socket->state(); if ("连接" == this->ui->pushConnect->text()) { QString ipAdd(this->ui->txtIp->text()), portd(this->ui->txtPort->text()); if (ipAdd.isEmpty() || portd.isEmpty()) { this->ui->textEdit->append("请输入IP和端口!"); return; } socket->connectToHost(ipAdd,portd.toInt()); this->ui->pushConnect->setEnabled(false); } else { socket->disconnectFromHost(); } }
void MainWindow::on_pushConnect_clicked() { qDebug() << "点击连接:" ; if ("连接" == this->ui->pushConnect->text()) { QString ipAdd(this->ui->txtIp->text()), portd(this->ui->txtPort->text()); if (ipAdd.isEmpty() || portd.isEmpty()) { this->ui->textEdit->append("请输入IP和端口!"); return; } tcpClient->connectToHost(ipAdd,portd.toInt()); if (tcpClient->waitForConnected(1000)) { ui->pushConnect->setText("断开"); ui->textEdit->append("连接服务器成功"); ui->pushSent->setEnabled(true); this->ui->txtIp->setEnabled(false); this->ui->txtPort->setEnabled(false); this->ui->timeBut->setEnabled(true); } } else { tcpClient->disconnectFromHost(); if (tcpClient->state() == QAbstractSocket::UnconnectedState || tcpClient->waitForDisconnected(1000) ) { ui->pushConnect->setText("连接"); ui->textEdit->append("断开服务器"); ui->pushSent->setEnabled(false); this->ui->txtIp->setEnabled(true); this->ui->txtPort->setEnabled(true); tm.stop(); this->ui->timeBut->setEnabled(false); this->ui->lineEdit->setEnabled(true); this->ui->timeBut->setText("启动定时"); } } }