修改NetControl的初始化逻辑,修复关闭软件时NetControl线程析构时的问题

LanXin3D
QuShuailong 2 years ago
parent c6d7805f0b
commit e51da4f96e

@ -8,19 +8,11 @@ NetControl::NetControl(const QString& configPath, bool bAutoReconnect /* = false
{ {
qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError"); qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError");
initIpAddress(m_configPath); 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() NetControl::~NetControl()
{ {
qDebug() << "delete netControl";
if (m_pTimerHeartbeat) if (m_pTimerHeartbeat)
{ {
m_pTimerHeartbeat->stop(); m_pTimerHeartbeat->stop();
@ -34,6 +26,7 @@ NetControl::~NetControl()
m_pTcpSocket->deleteLater(); m_pTcpSocket->deleteLater();
m_pTcpSocket = nullptr; m_pTcpSocket = nullptr;
} }
qDebug() << "delete netControl finished";
} }
bool NetControl::initIpAddress(const QString& path) bool NetControl::initIpAddress(const QString& path)
@ -74,6 +67,19 @@ bool NetControl::initIpAddress(const QString& path)
return true; 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) void NetControl::connectNet(QString severIp, int serverPort)
{ {
m_pTcpSocket->connectToHost(severIp, serverPort); m_pTcpSocket->connectToHost(severIp, serverPort);

@ -24,6 +24,7 @@ public:
~NetControl(); ~NetControl();
public: public:
//void deCodeMsg(QString recInfo); //void deCodeMsg(QString recInfo);
Q_SLOT void onInitNet();
void waitForConnected(int msec); void waitForConnected(int msec);
Q_SLOT void onSendMsg(const QString& msg); Q_SLOT void onSendMsg(const QString& msg);

@ -37,9 +37,13 @@ SmokeBoxIdentification::SmokeBoxIdentification(QWidget *parent)
m_pNumTemplateBuild->setDataProcessObj(m_pDataProcess); m_pNumTemplateBuild->setDataProcessObj(m_pDataProcess);
} }
QString filePath = applicationDirPath + NET_CONFIG; QString filePath = applicationDirPath + NET_CONFIG;
m_pNetControl = new NetControl(filePath, true); m_pNetControl = new NetControl(filePath, true, this);
m_pTcpClientThread = new QThread(); m_pTcpClientThread = new QThread();
m_pNetControl->moveToThread(m_pTcpClientThread); 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(); m_pTcpClientThread->start();
connect(m_pNetControl, &NetControl::sgReceiveData, this, &SmokeBoxIdentification::onDecodeMsg, Qt::QueuedConnection); connect(m_pNetControl, &NetControl::sgReceiveData, this, &SmokeBoxIdentification::onDecodeMsg, Qt::QueuedConnection);
connect(this, &SmokeBoxIdentification::sgSendMsg, m_pNetControl, &NetControl::onSendMsg, Qt::QueuedConnection); connect(this, &SmokeBoxIdentification::sgSendMsg, m_pNetControl, &NetControl::onSendMsg, Qt::QueuedConnection);
@ -80,8 +84,8 @@ SmokeBoxIdentification::SmokeBoxIdentification(QWidget *parent)
SmokeBoxIdentification::~SmokeBoxIdentification() SmokeBoxIdentification::~SmokeBoxIdentification()
{ {
emit sgControlSideLight(lightCtrl::emTurnOffLight); emit sgControlSideLight(lightCtrl::emTurnOffLight);
DELETE_POINTER(m_pNetControl);
DELETE_POINTER(m_pCategoryMatcher); DELETE_POINTER(m_pCategoryMatcher);
DELETE_POINTER(m_pNetControl);
if (m_pTcpClientThread) if (m_pTcpClientThread)
{ {
m_pTcpClientThread->quit(); m_pTcpClientThread->quit();

Loading…
Cancel
Save