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();