From 84cbe95aedfd915a6580e638b229d25415903b8d Mon Sep 17 00:00:00 2001 From: Msy-msu <2200456405@qq.com> Date: Sun, 15 Jan 2023 17:17:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=81=9A=E4=BA=86=E4=B8=80=E4=BA=9B=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TcpClient.pro.user | 2 +- datamanager.cpp | 1 + mainwidget.cpp | 14 +++++++------- tcpclient.cpp | 8 +++++++- tcpclient.h | 2 ++ 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/TcpClient.pro.user b/TcpClient.pro.user index 066b750..8416732 100644 --- a/TcpClient.pro.user +++ b/TcpClient.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/datamanager.cpp b/datamanager.cpp index 487d1b0..793d1f9 100644 --- a/datamanager.cpp +++ b/datamanager.cpp @@ -9,6 +9,7 @@ DataManager::DataManager(QObject *parent) connect(tcpclient, &TCPClient::TCPClientDisconnected, this, &DataManager::TCPClientDisconnected); connect(tcpclient, &TCPClient::recv_data, this, &DataManager::recv_from_server); tcpclient->setServer("ourdocs.cn",35137); + tcpclient->setTimeOut(500); tcpclient->configAndrun(); // tcpclient->setCallBack(recv_from_server); protocol = Protocol::getInstance(); diff --git a/mainwidget.cpp b/mainwidget.cpp index 01ceb37..1ec8269 100644 --- a/mainwidget.cpp +++ b/mainwidget.cpp @@ -78,7 +78,7 @@ void MainWidget::on_send_text_clicked() QString currentTime = QTime::currentTime().toString("H:mm A"); QString html = "" + filename + ""; qDebug() << html; - ui->textEdit->append("" + username + " [" + currentTime + "] send a file \"" + html + "\""); + ui->textEdit->append("

" + username + " [" + currentTime + "] send a file \"" + html + "\"

"); this->sendFile(filePath,filename); } else //私发文件 @@ -108,7 +108,7 @@ void MainWidget::on_send_text_clicked() QString currentTime = QTime::currentTime().toString("H:mm A"); QString html = "" + filename + ""; qDebug() << html; - ui->textEdit->append("" + username + " [" + currentTime + "] send " + name + " a private file \"" + html + "\""); + ui->textEdit->append("

" + username + " [" + currentTime + "] send " + name + " a private file \"" + html + "\"

"); this->sendFile(filePath,filename,name); } } @@ -207,14 +207,14 @@ void MainWidget::on_send_btn_clicked() else { QString currentTime = QTime::currentTime().toString("H:m A"); - ui->textEdit->append("" + username + " [" + currentTime + "] " + text_self + ""); + ui->textEdit->append("

" + username + " [" + currentTime + "] " + text_self + "

"); emit send_to_user(username,to_user,data_text); } } else { QString currentTime = QTime::currentTime().toString("H:m A"); - ui->textEdit->append("" + username + " [" + currentTime + "] : " + text + ""); + ui->textEdit->append("

" + username + " [" + currentTime + "] : " + text + "

"); emit send_text(username,text); } } @@ -272,7 +272,7 @@ void MainWidget::on_stop_clicked() void MainWidget::update_text(QString sendname,QString data) { QString currentTime = QTime::currentTime().toString("H:m A"); - ui->textEdit->append("" + sendname + " [" + currentTime + "] : " + data + ""); + ui->textEdit->append("

" + sendname + " [" + currentTime + "] : " + data + "

"); qDebug() << sendname + " [" + currentTime + "] : " + data; } @@ -281,7 +281,7 @@ void MainWidget::update_file(QString sendname,QString filename,QString file_data readFile(filename,QByteArray::fromHex(file_data.toUtf8())); QString currentTime = QTime::currentTime().toString("H:m A"); QString html = "" + filename + ""; - QString msg = "" + username + " [" + currentTime + "] : " + QString("收到了一个来自%1的群发文件:").arg(sendname) + html + ""; + QString msg = "

" + username + " [" + currentTime + "] : " + QString("收到了一个来自%1的群发文件:").arg(sendname) + html + "

"; ui->textEdit->append(msg); qDebug() << msg; } @@ -291,7 +291,7 @@ void MainWidget::update_file_private(QString sendname,QString filename,QString f readFile(filename,QByteArray::fromHex(file_data.toUtf8())); QString currentTime = QTime::currentTime().toString("H:m A"); QString html = "" + filename + ""; - QString msg = "" + username + " [" + currentTime + "] : " + QString("收到了一个来自%1的私发文件:").arg(sendname) + html + ""; + QString msg = "

" + username + " [" + currentTime + "] : " + QString("收到了一个来自%1的私发文件:").arg(sendname) + html + "

"; ui->textEdit->append(msg); qDebug() << msg; } diff --git a/tcpclient.cpp b/tcpclient.cpp index 8c9ab48..e688c2e 100644 --- a/tcpclient.cpp +++ b/tcpclient.cpp @@ -70,7 +70,7 @@ void TCPClient::readSocket() while(socket.bytesAvailable())//循环接收 { recv += socket.readAll(); - if (!socket.waitForReadyRead(500))//超时停止接收 + if (!socket.waitForReadyRead(_timeout))//超时停止接收 { /*QString error = sock->errorString(); string error_t = error.toStdString(); @@ -87,6 +87,12 @@ void TCPClient::readSocket() emit recv_data(recv); } +void TCPClient::setTimeOut(int timeout) +{ + _timeout = timeout; +} + + void TCPClient::setCallBack(void (*callback)(QString &mac, quint16 port, QByteArray &data)) { _callback = callback; diff --git a/tcpclient.h b/tcpclient.h index 42aa6ca..679a080 100644 --- a/tcpclient.h +++ b/tcpclient.h @@ -14,6 +14,7 @@ public: void configAndrun(); void setCallBack(void (*callback)(QString &ip, quint16 port,QByteArray &data)); void setServer(QString IP, quint16 port); + void setTimeOut(int timeout); private slots: void readSocket(); void connected(); @@ -24,6 +25,7 @@ private: static void _sleep(int msec); static QTcpSocket socket; static TCPClient tcpclient_instance; + int _timeout = 20; QString server_ip = "127.0.0.1"; quint16 server_port = 7890; bool has_found = false;