Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved]I have a problem about the Qthread
QtWS25 Last Chance

[solved]I have a problem about the Qthread

Scheduled Pinned Locked Moved General and Desktop
qthread threads
6 Posts 2 Posters 3.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joeQ
    wrote on 5 Jul 2015, 04:27 last edited by joeQ 7 May 2015, 08:42
    #1

    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);
    }
    
    

    Just do it!

    M 1 Reply Last reply 5 Jul 2015, 08:12
    0
    • J joeQ
      5 Jul 2015, 04:27

      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);
      }
      
      
      M Offline
      M Offline
      mcosta
      wrote on 5 Jul 2015, 08:12 last edited by
      #2

      @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)

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      1
      • J Offline
        J Offline
        joeQ
        wrote on 5 Jul 2015, 08:41 last edited by
        #3

        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!

        Just do it!

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on 5 Jul 2015, 08:43 last edited by mcosta 7 May 2015, 08:43
          #4

          How did you solved??

          Posting the solution can help other people with the same problem

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          J 1 Reply Last reply 5 Jul 2015, 09:04
          0
          • M mcosta
            5 Jul 2015, 08:43

            How did you solved??

            Posting the solution can help other people with the same problem

            J Offline
            J Offline
            joeQ
            wrote on 5 Jul 2015, 09:04 last edited by
            #5

            @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?

            Just do it!

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on 5 Jul 2015, 09:09 last edited by
              #6

              Yes, Thank you

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              1 Reply Last reply
              0

              1/6

              5 Jul 2015, 04:27

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved