求助:qt线程中使用QTcpSocket发送数据报错
兔腿 2018-07-18 09:21:02 创建了一个线程,在run函数里new了一个QTcpSocket,在线程定时器响应函数里周期性使用QTcpSocket发送数据,write时报如下错误:
QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
查了一下,说是QTcpSocket的创建和使用要在同一个线程里,但是我的创建和使用是在一个线程里啊,为什么还是报错?
代码如下:
void AliveScoketThr::run()
{
m_AliveSocket =new QTcpSocket(this);
m_AliveSocket->connectToHost("192.168.0.10",15433,QTcpSocket::ReadWrite);
connect(m_AliveSocket,SIGNAL(connected()),this,SLOT(on_alive_connected()));
msleep(10);
QTimer * pTimer = new QTimer;
pTimer->setInterval(30);
connect(pTimer , SIGNAL(timeout()) , this , SLOT(on_alive_timer())) ;
pTimer->start() ;
exec();
}
void AliveScoketThr::on_alive_timer()
{
char buff[12];
buff[0] = 9;
unsigned long *pLen = (unsigned long *)(buff+1);
*pLen = 3;
strcpy(buff+5,"any");
m_AliveSocket->write(buff,8); //该处报错
}
求助啊,各位大侠帮帮忙。