• 7772阅读
  • 1回复

[提问]使用Qt多线程,发生QObject: Cannot create children for a parent that is in a different thread.错误 [复制链接]

上一主题 下一主题
离线三少爷
 
只看楼主 倒序阅读 楼主  发表于: 2012-05-14
我的程序是这样的, 服务器端有clientsocket类,dcThread类,server类,以及主界面类,程序加入用于改变界面的signal和slot之后,一直发生以下错误,找网上的办法都不行!!
[size=; font-size:10pt,10pt][font='Courier']QObject: Cannot create children for a parent that is in a different thread.
[size=; font-size:10pt,10pt][font='Courier'](Parent is DCSThread(0x13006d0), parent's thread is QThread(0x15e9ed0), current thread is DCSThread(0x13006d0)[size=; font-size:10pt,10pt][font='Courier']

本来在加入改变界面的signal和slot之前,这个错误通过添加Qt::DirectConnection参数解决,但是现在也不行了,请大家帮忙哈~~
以下是部分代码:
  1. #ifndef CLIENTSOCKET_H
  2. #define CLIENTSOCKET_H
  3. #include <QtNetwork/QTcpSocket>
  4. #include <QPixmap>
  5. #include <QImage>
  6. class ClientSocket : public QTcpSocket
  7. {
  8.     Q_OBJECT
  9. public:
  10.     explicit ClientSocket(QObject *parent = 0,int client_id=0);
  11.     void sendInfor();//发信息:N=正常,W=警告,B=阻塞,U=取消阻塞
  12. signals:
  13.     void createOne(int,QString,QString,QImage);
  14. public slots:
  15.     void readInfor();//读取:I=客户端信息+截图,P=截图,C=关机
  16. private:
  17.     quint16 nextBlockSize;
  18.     quint8 type;
  19.     int clientId;
  20.     //客户端信息
  21.     QString clientName;
  22.     QString clientIP;
  23.     //QPixmap clientPic;
  24.     QImage clientPic;
  25. };
  26. #endif // CLIENTSOCKET_H
clientsocket的实现:

  1. #include "clientsocket.h"
  2. #include <QMessageBox>
  3. #include <QDebug>
  4. ClientSocket::ClientSocket(QObject *parent,int client_id) :
  5.     QTcpSocket(parent),clientId(client_id)
  6. {
  7.     //type初始化
  8.     qDebug()<<"start a socket!";
  9.     type='N';
  10.     connect(this,SIGNAL(readyRead()),this,SLOT(readInfor()),Qt::DirectConnection);
  11.     connect(this,SIGNAL(disconnected()),this,SLOT(deleteLater()));
  12.     nextBlockSize=0;
  13.     qDebug()<<"client_id:"<<clientId;
  14. }
  15. void ClientSocket::sendInfor()//发信息:N=正常,W=警告,B=阻塞,U=取消阻塞
  16. {
  17.     //服务器发信息:大小+类型
  18.     QByteArray block;
  19.     QDataStream out(&block,QIODevice::WriteOnly);
  20.     out.setVersion(QDataStream::Qt_4_8);
  21.     out<<quint16(0)<<type;
  22.     out.device()->seek(0);
  23.     out<<quint16(block.size()-sizeof(quint16));
  24.     write(block);
  25.     nextBlockSize=0;
  26. }
  27. void ClientSocket::readInfor()//读取:I=客户端信息+截图,P=截图,C=关机
  28. {
  29.     qDebug()<<"read infor!";
  30.     //服务器收到信息:大小+类型+内容,除了C没有内容
  31.     QDataStream in(this);
  32.     in.setVersion(QDataStream::Qt_4_8);
  33.     if(nextBlockSize==0)
  34.     {
  35.         if(bytesAvailable()<sizeof(quint16)){
  36.             return;
  37.         }
  38.         in>>nextBlockSize;
  39.         qDebug()<<nextBlockSize;
  40.     }
  41.     in>>type;
  42.     qDebug()<<type;
  43.     switch (type)
  44.     {
  45.     case 'I':
  46.         //客户端信息:显示图片和计算机名字等
  47.         in>>clientName>>clientIP;
  48.         //暂且不考虑截图切割比较以及huffman编码压缩等,直接读取图片
  49.         in>>clientPic;
  50.         qDebug()<<clientName<<"  "<<clientIP;
  51.         qDebug()<<clientPic.size();
  52.         //应该弄一个信号,由于这个信号,引发建立一个图片,显示名字,connect信号和信号等
  53.         //emit createOne(clientId,clientName,clientIP,clientPic);//发送信号
  54.         //qDebug()<<"emit createOne()--socket";
  55.         type='N';//test
  56.         break;
  57.     case 'P':
  58.         //截图,显示变化
  59.         qDebug()<<"should p";
  60.         type='N';
  61.         break;
  62.     case 'C':
  63.         //显示关闭图片
  64.         break;
  65.     default:
  66.         break;
  67.     }
  68.     sendInfor();
  69. }
DCThread类

  1. #ifndef DCSTHREAD_H
  2. #define DCSTHREAD_H
  3. #include <QThread>
  4. #include <QtNetwork/QTcpSocket>
  5. #include "clientsocket.h"
  6. class DCSThread : public QThread
  7. {
  8.     Q_OBJECT
  9. public:
  10.     explicit DCSThread(int handle,QObject *parent = 0,int clientid=0);
  11. signals:
  12.     void createOne(int,QString,QString,QImage);
  13. public slots:
  14. protected:
  15.     void run();
  16. private:
  17.     ClientSocket *tcpSocket;
  18.     int socketID;
  19.     int clientID;
  20. };
  21. #endif // DCSTHREAD_H
  1. #include "dcsthread.h"
  2. #include <QDebug>
  3. DCSThread::DCSThread(int handle,QObject *parent,int clientid) :
  4.     QThread(parent),socketID(handle),clientID(clientid)
  5. {
  6.     qDebug()<<"start a thread!";
  7.     //clientID=11;
  8. }
  9. void DCSThread::run()
  10. {
  11.     qDebug()<<"run thread!";
  12.     tcpSocket=new ClientSocket(this,clientID);
  13.     tcpSocket->setSocketDescriptor(socketID);
  14.     //socket的信号和thread的信号相连
  15.     //connect(tcpSocket,SIGNAL(createOne(int,QString,QString,QImage)),this,SIGNAL(createOne(int,QString,QString,QImage)));
  16.     exec();
  17. }
server类
  1. #ifndef DCTCPSERVER_H
  2. #define DCTCPSERVER_H
  3. #include <QtNetwork/QTcpServer>
  4. #include "dcsthread.h"
  5. class DCTcpServer : public QTcpServer
  6. {
  7.     Q_OBJECT
  8. public:
  9.     explicit DCTcpServer(QObject *parent = 0);
  10. signals:
  11.     void createOne(int,QString,QString,QImage);
  12. public slots:
  13. private:
  14.     void incomingConnection(int handle);
  15.     int client_count;
  16. };
  17. #endif // DCTCPSERVER_H
  1. #include "dctcpserver.h"
  2. #include <QDebug>
  3. DCTcpServer::DCTcpServer(QObject *parent) :
  4.     QTcpServer(parent)
  5. {
  6.     qDebug()<<"server start!";
  7.     client_count=0;
  8. }
  9. void DCTcpServer::incomingConnection(int handle)
  10. {
  11.     client_count++;
  12.     DCSThread *serverThread=new DCSThread(handle,this,client_count);
  13.     connect(serverThread,SIGNAL(finished()),serverThread,SLOT(deleteLater()));
  14.     //thread的信号和server的信号相连
  15.     //connect(serverThread,SIGNAL(createOne(int,QString,QString,QImage)),this,SIGNAL(createOne(int,QString,QString,QImage)));
  16.     serverThread->start();
  17. }
然后就是界面类中有一个对应的createOne(...)slot。
我已经把signal和slot只定义了,但是没有使用,但是还是出现这个错误。
其实代码大体上就是普通的,socket类,thread类,server类,界面类,thread的run()中,定义socket,然后执行
请大家帮忙,Qt多线程这个错误到底怎么结局??
谢谢大家!!



离线三少爷
只看该作者 1楼 发表于: 2012-05-14
Re:使用Qt多线程,发生QObject: Cannot create children for a parent that is in a different thread. ..
啊,发现了一个新问题,比较诡异:
当改变程序代码后,第一次执行,程序不会出现这个多线程的问题,但是第二次执行开始,立马出现提到的多线程问题,然后不能执行了!!!这是怎么回事??
快速回复
限100 字节
 
上一个 下一个