首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > QT开发 >

qt中socket指针跨线程有关问题

2012-04-04 
qt中socket指针跨线程问题C/C++ code#include serverThread.h#include mainpanel.h#include QtNetwor

qt中socket指针跨线程问题

C/C++ code
#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();    }}

我在槽函数judge();里调用sendMessage();说我socket指针跨线程了,我的socket是在run里new的啊,为什么跨线程了,求指点

[解决办法]
你线程是怎么启动的?
[解决办法]
看看有没有可能在子线程中修改了UI之类的东西。
[解决办法]
子线程不能操作UI
探讨

看看有没有可能在子线程中修改了UI之类的东西。

[解决办法]
探讨

子线程不能操作UI
引用:

看看有没有可能在子线程中修改了UI之类的东西。

热点排行