From e51da4f96e92d8a9cc82a8ff54f9824ea681de04 Mon Sep 17 00:00:00 2001 From: QuShuailong Date: Thu, 12 Oct 2023 11:37:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9NetControl=E7=9A=84=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E8=BD=AF=E4=BB=B6=E6=97=B6NetControl?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E6=9E=90=E6=9E=84=E6=97=B6=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/NetControl.cpp | 24 +++++++++++++++--------- src/NetControl.h | 1 + src/SmokeBoxIdentification.cpp | 8 ++++++-- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/NetControl.cpp b/src/NetControl.cpp index 0bc52cd..4438b38 100644 --- a/src/NetControl.cpp +++ b/src/NetControl.cpp @@ -8,19 +8,11 @@ NetControl::NetControl(const QString& configPath, bool bAutoReconnect /* = false { qRegisterMetaType("QAbstractSocket::SocketError"); initIpAddress(m_configPath); - m_pTimerHeartbeat = new QTimer(this); - connect(m_pTimerHeartbeat, &QTimer::timeout, this, &NetControl::sendHeartbeatPack); - m_pTcpSocket = new QTcpSocket(this); - connect(m_pTcpSocket, SIGNAL(connected()), this, SLOT(onConnected())); - connect(m_pTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), - this, SLOT(onErrorHandler(QAbstractSocket::SocketError))); - connect(m_pTcpSocket, SIGNAL(readyRead()), this, SLOT(onMsgReceived())); - connect(m_pTcpSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected())); - connectNet(m_szHostIp, m_port); } NetControl::~NetControl() { + qDebug() << "delete netControl"; if (m_pTimerHeartbeat) { m_pTimerHeartbeat->stop(); @@ -34,6 +26,7 @@ NetControl::~NetControl() m_pTcpSocket->deleteLater(); m_pTcpSocket = nullptr; } + qDebug() << "delete netControl finished"; } bool NetControl::initIpAddress(const QString& path) @@ -74,6 +67,19 @@ bool NetControl::initIpAddress(const QString& path) return true; } +Q_SLOT void NetControl::onInitNet() +{ + m_pTimerHeartbeat = new QTimer(this); + connect(m_pTimerHeartbeat, &QTimer::timeout, this, &NetControl::sendHeartbeatPack); + m_pTcpSocket = new QTcpSocket(this); + connect(m_pTcpSocket, SIGNAL(connected()), this, SLOT(onConnected())); + connect(m_pTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), + this, SLOT(onErrorHandler(QAbstractSocket::SocketError))); + connect(m_pTcpSocket, SIGNAL(readyRead()), this, SLOT(onMsgReceived())); + connect(m_pTcpSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected())); + connectNet(m_szHostIp, m_port); +} + void NetControl::connectNet(QString severIp, int serverPort) { m_pTcpSocket->connectToHost(severIp, serverPort); diff --git a/src/NetControl.h b/src/NetControl.h index e6e6171..bdd37f1 100644 --- a/src/NetControl.h +++ b/src/NetControl.h @@ -24,6 +24,7 @@ public: ~NetControl(); public: //void deCodeMsg(QString recInfo); + Q_SLOT void onInitNet(); void waitForConnected(int msec); Q_SLOT void onSendMsg(const QString& msg); diff --git a/src/SmokeBoxIdentification.cpp b/src/SmokeBoxIdentification.cpp index b080680..a9d55d7 100644 --- a/src/SmokeBoxIdentification.cpp +++ b/src/SmokeBoxIdentification.cpp @@ -37,9 +37,13 @@ SmokeBoxIdentification::SmokeBoxIdentification(QWidget *parent) m_pNumTemplateBuild->setDataProcessObj(m_pDataProcess); } QString filePath = applicationDirPath + NET_CONFIG; - m_pNetControl = new NetControl(filePath, true); + m_pNetControl = new NetControl(filePath, true, this); m_pTcpClientThread = new QThread(); m_pNetControl->moveToThread(m_pTcpClientThread); + connect(m_pTcpClientThread, &QThread::started, + m_pNetControl, &NetControl::onInitNet); + connect(m_pTcpClientThread, &QThread::finished, + this, &QObject::deleteLater); m_pTcpClientThread->start(); connect(m_pNetControl, &NetControl::sgReceiveData, this, &SmokeBoxIdentification::onDecodeMsg, Qt::QueuedConnection); connect(this, &SmokeBoxIdentification::sgSendMsg, m_pNetControl, &NetControl::onSendMsg, Qt::QueuedConnection); @@ -80,8 +84,8 @@ SmokeBoxIdentification::SmokeBoxIdentification(QWidget *parent) SmokeBoxIdentification::~SmokeBoxIdentification() { emit sgControlSideLight(lightCtrl::emTurnOffLight); - DELETE_POINTER(m_pNetControl); DELETE_POINTER(m_pCategoryMatcher); + DELETE_POINTER(m_pNetControl); if (m_pTcpClientThread) { m_pTcpClientThread->quit();