[solved]I have a problem about the Qthread



  • First my error is

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QNativeSocketEngine(0xbbb9d0), parent's thread is QThread(0xbb1768),
    current thread is QThread(0xbbc198)
    

    and it is a running time error.and i also asked someone,he said, it is about my Qt version,and ,it will not have the problem when use the Qt 3.8,but my Qt is 5.4.0 . is it real about the Qt version? Thank you very much!

    my code is:

    main.cpp

    #include <QCoreApplication>
    #include "myserver.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        MyServer server;
        server.ServerStart();
    
        return a.exec();
    }
    
    

    myserver.h

    #ifndef MYSERVER_H
    #define MYSERVER_H
    
    #include <QObject>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QAbstractSocket>
    #include <QDebug>
    
    class MyServer : public QTcpServer//inherit QTcpServer
    {
        Q_OBJECT
    public:
        explicit MyServer(QObject *parent = 0 );
        void ServerStart();
    protected:
        void incomingConnection(int handl);
    };
    
    #endif // MYSERVER_H
    

    myserver.cpp

    #include "myserver.h"
    #include "myclient.h"
    MyServer::MyServer(QObject *parent):QTcpServer(parent)
    {
    
    }
    
    void MyServer::ServerStart()
    {
        if( this->listen(QHostAddress::Any,1234))
        {
            qDebug()<<"Connectioned!";
        }
        else
        {
            qDebug()<<"no Connection";
        }
    }
    
    void MyServer::incomingConnection(int handl)
    {
        MyClient *client = new MyClient(this);
        client->setSocket(handl);
    }
    
    

    myclient.h

    #ifndef MYCLIENT_H
    #define MYCLIENT_H
    
    #include <QObject>
    #include <QTcpSocket>
    #include <QThreadPool>
    
    class MyClient : public QObject
    {
        Q_OBJECT
    public:
        explicit MyClient(QObject *parent = 0);
        void setSocket(int socketDescriptior);
    signals:
    
    public slots:
        void connected();
        void disconnected();
        void readyRead();
        void TaskResult(int Number);
    private:
        QTcpSocket *socket;
        QThreadPool *threadPool;
    };
    
    #endif // MYCLIENT_H
    

    myclient.cpp

    #include <QThreadPool>
    #include "myclient.h"
    #include "mytask.h"
    
    MyClient::MyClient(QObject *parent) : QObject(parent)
    {
        QThreadPool::globalInstance()->setMaxThreadCount(5);
        //this->threadPool = new QThreadPool(this);
        //this->threadPool->setMaxThreadCount(5);
    }
    
    void MyClient::setSocket(int socketDescriptior)
    {
        this->socket = new QTcpSocket(this);
        connect(socket,SIGNAL(connected()),this,SLOT(connected()));
        connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
        connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
        this->socket->setSocketDescriptor(socketDescriptior);
        qDebug()<<"Client connected!";
    }
    
    void MyClient::connected()
    {
        qDebug()<<"Client connected event!";
    }
    
    void MyClient::disconnected()
    {
        qDebug()<<"Client disconnected event!";
    }
    
    void MyClient::readyRead()
    {
        qDebug()<<socket->readAll();
        MyTask *task = new MyTask();
        task->setAutoDelete(true);
        connect(task,SIGNAL(Result(int)),this,SLOT(TaskResult(int)),Qt::DirectConnection);
        QThreadPool::globalInstance()->start(task);
        //this->threadPool->start(task);
    }
    
    void MyClient::TaskResult(int Number)
    {
        QByteArray buffer;
        buffer.append(tr("\r\nTask Result : "));
        buffer.append(QString::number(Number));
        this->socket->write(buffer);
    }
    
    

    mytask.h

    #ifndef MYTASK_H
    #define MYTASK_H
    #include <QRunnable>
    #include <QObject>
    #include <QDebug>
    
    class MyTask : public QObject,public QRunnable
    {
        Q_OBJECT
    public:
        MyTask();
    signals:
        void Result(int iNumber);
    
    protected:
        void run();
    };
    
    #endif // MYTASK_H
    

    mytask.cpp

    #include "mytask.h"
    
    MyTask::MyTask()
    {
    
    }
    
    void MyTask::run()
    {
        qDebug()<<"Tesk Run...";
        int iNumber = 0;
        for(int i=0; i<100; ++i)
        {
            iNumber += i;
        }
        qDebug()<<"Task done!";
        emit Result(iNumber);
    }
    
    


  • @joeQ said:

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QNativeSocketEngine(0xbbb9d0), parent's thread is QThread(0xbb1768),
    current thread is QThread(0xbbc198)

    The message is clear; to try to understand you can print the Thread identifiers.
    The only strange thing I see in your code is

    connect(task,SIGNAL(Result(int)),this,SLOT(TaskResult(int)),Qt::DirectConnection);

    try to not use a DirectConnection, better QueuedConnection or Auto (the default)



  • Very,Very,Very,Thank you.I am sorry about my error,and I am a new person about to learn Qt.ok, my problem is done. i will see document to know why.Thank you!



  • How did you solved??

    Posting the solution can help other people with the same problem



  • @mcosta You told me,the problem is at here
    connect(task,SIGNAL(Result(int)),this,SLOT(TaskResult(int)),Qt::DirectConnection);
    so,i modified it
    connect(task,SIGNAL(Result(int)),this,SLOT(TaskResult(int)));
    and I see the document and i know why.
    the document said:

    *Qt::DirectConnection	
    The slot is invoked immediately when the signal is emitted. The slot is executed *in the signalling thread*. 
    
    *Qt::QueuedConnection	  
    The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.
    
    *Qt::AutoConnection	
    (Default) If the receiver lives in the thread that emits the signal, Qt::DirectConnection is used. Otherwise, Qt::QueuedConnection is used. The connection type is determined when the signal is emitted.
    

    when i used the DirectConnection,the slot was executed in the signalling thread,and the signalling thread is another thread,and in mytask.cpp,so,I was wrong at here.

    is it right about my said?



  • Yes, Thank you


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.