qt中socket指针跨线程问题
#include "serverThread.h"#include "mainpanel.h"#include <QtNetwork>#include <QTimer>serverThread::serverThread(int socketDescriptor,QObject *parent) :QThread(parent),socketDescriptor(socketDescriptor){ dsv = (airServer *)parent;}void serverThread::run(){ tcpSocket=new QTcpSocket; if (!tcpSocket->setSocketDescriptor(socketDescriptor)) { emit error(tcpSocket->error()); return; } if(dsv->dlg->getMode() == 0) { preMode=0; curMode=0; } else if(dsv->dlg->getMode() == 1) { preMode=1; curMode=1; } sendMessage(); connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readSocket())); timer=new QTimer; timer->start(1000); connect(timer,SIGNAL(timeout()),this,SLOT(judge())); exec();}void serverThread::ntoc(int num){ msg1[2]=num/10+'0'; msg1[3]=num%10+'0';}void serverThread::readSocket(){ char getmsg[20]={0}; tcpSocket->read(getmsg,20); qDebug("&&&&%s",getmsg); int n; n=(getmsg[0]-'0')*10+(getmsg[1]-'0'); qDebug("%d",n); if(getmsg[11]=='B') { dsv->dlg->ifwork[n]=1; } else { dsv->dlg->ifwork[n]=0; } if(dsv->dlg->judge()==0) dsv->dlg->setStatusSB(); else dsv->dlg->setStatusWork(); if(getmsg[11]=='D') { // tcpSocket->disconnectFromHost(); // tcpSocket->waitForDisconnected(); qDebug("duan"); quit(); }}void serverThread::sendMessage(){ memset(msg1,0,20); if(curMode == 0) { msg1[0]='L'; } else if(curMode == 1) { msg1[0]='H'; } msg1[1]=' '; ntoc(dsv->dlg->getLastNum()); msg1[4]='0'; msg1[5]=' '; qDebug("~%s",msg1); tcpSocket->write(msg1);}void serverThread::judge(){ if(dsv->dlg->getMode() == 0) { preMode=curMode; curMode=0; } else if(dsv->dlg->getMode() == 1) { preMode=curMode; curMode=1; } if(curMode!=preMode) sendMessage(); if(dsv->dlg->ifclose==0) { tcpSocket->disconnectFromHost(); tcpSocket->waitForDisconnected(); }}