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. QObject: cannot create children for a parent which is in a different thread
Forum Updated to NodeBB v4.3 + New Features

QObject: cannot create children for a parent which is in a different thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 395 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.
  • S Offline
    S Offline
    Subuday
    wrote on last edited by Subuday
    #1

    When I add to my code it causes the error: "QObject: cannot create children for a parent which is in a different thread"

           socket->write("From server: hello world");
           socket->flush();
           socket->waitForBytesWritten();
    
    #include "httpserver.h"
    
    HttpServer::HttpServer(QObject *parent) : QTcpServer(parent)
    {
        pool = new QThreadPool(this);
    }
    
    HttpServer::~HttpServer()
    {
        delete pool;
    }
    
    void HttpServer::setStorage(Storage *storage)
    {
      this->storage = storage;
    }
    
    void HttpServer::startServer()
    {
        if(listen(QHostAddress::Any, 1234))
        {
            qDebug() << "Server started";
        }
        else
        {
            qDebug() << "Server: not started";
        }
    
    }
    
    void HttpServer::incomingConnection(qintptr socketDescriptor)
    {
        Runnable *task = new Runnable();
    
        task->setAutoDelete(true);
        task->socketDescriptor = socketDescriptor;
        task->storage = storage;
    
        pool->start(task);
    }
    
    

    //Runnable

    #include "runnable.h"
    
    Runnable::Runnable(QObject *parent) :
        QObject(parent)
    {
    
    }
    
    void Runnable::run()
    {
        eventLoop = new QEventLoop();
        socket = new QTcpSocket();
        if(socket->setSocketDescriptor(socketDescriptor))
        {
            connect(socket,SIGNAL(connected()),this,SLOT(onConnected()),Qt::QueuedConnection);
            connect(socket,SIGNAL(disconnected()),this,SLOT(onDisconnect()),Qt::QueuedConnection);
            connect(socket,SIGNAL(readyRead()),this,SLOT(onReadyRead()),Qt::QueuedConnection);
        }
        eventLoop->exec();
        delete socket;
        delete eventLoop;
    }
    
    void Runnable::onConnected()
    {
        qDebug() << "Connected";
    }
    
    void Runnable::onDisconnect()
    {
        qDebug() << "Disconnected";
        eventLoop->exit();
    }
    
    void Runnable::onReadyRead()
    {
        parse(socket->readAll());
    }
    
    void Runnable::parse(QString httpRequest)
    {
        qDebug().noquote()   << httpRequest;
        QRegularExpression methodRegExp("\\b(GET|POST)\\b");
        QRegularExpressionMatch matchMethod = methodRegExp.match(httpRequest);
    
        if(matchMethod.hasMatch())
        {
            QString method = matchMethod.captured(0);
            if(method == "GET")
            {
    
            }
            else if(method == "POST")
            {
                QRegularExpression uriRegExp("/(.*?)\\s");
                QRegularExpressionMatch matchURI = uriRegExp.match(httpRequest);
    
                QString uri = matchURI.captured(0);
                uri.truncate(uri.lastIndexOf(QChar(' ')));
                qDebug() << uri;
    
                QRegularExpression bodyRegExp("(\\[.*?\\]|\\{.*?\\})");
                QRegularExpressionMatch matchBody = bodyRegExp.match(httpRequest);
    
                qDebug() << matchBody;
    
                socket->write("From server: hello world");
                socket->flush();
                socket->waitForBytesWritten();
            }
        }
        else
        {
            qDebug() << "No match";
        }
    }
    
    
    jsulmJ 1 Reply Last reply
    0
    • S Subuday

      When I add to my code it causes the error: "QObject: cannot create children for a parent which is in a different thread"

             socket->write("From server: hello world");
             socket->flush();
             socket->waitForBytesWritten();
      
      #include "httpserver.h"
      
      HttpServer::HttpServer(QObject *parent) : QTcpServer(parent)
      {
          pool = new QThreadPool(this);
      }
      
      HttpServer::~HttpServer()
      {
          delete pool;
      }
      
      void HttpServer::setStorage(Storage *storage)
      {
        this->storage = storage;
      }
      
      void HttpServer::startServer()
      {
          if(listen(QHostAddress::Any, 1234))
          {
              qDebug() << "Server started";
          }
          else
          {
              qDebug() << "Server: not started";
          }
      
      }
      
      void HttpServer::incomingConnection(qintptr socketDescriptor)
      {
          Runnable *task = new Runnable();
      
          task->setAutoDelete(true);
          task->socketDescriptor = socketDescriptor;
          task->storage = storage;
      
          pool->start(task);
      }
      
      

      //Runnable

      #include "runnable.h"
      
      Runnable::Runnable(QObject *parent) :
          QObject(parent)
      {
      
      }
      
      void Runnable::run()
      {
          eventLoop = new QEventLoop();
          socket = new QTcpSocket();
          if(socket->setSocketDescriptor(socketDescriptor))
          {
              connect(socket,SIGNAL(connected()),this,SLOT(onConnected()),Qt::QueuedConnection);
              connect(socket,SIGNAL(disconnected()),this,SLOT(onDisconnect()),Qt::QueuedConnection);
              connect(socket,SIGNAL(readyRead()),this,SLOT(onReadyRead()),Qt::QueuedConnection);
          }
          eventLoop->exec();
          delete socket;
          delete eventLoop;
      }
      
      void Runnable::onConnected()
      {
          qDebug() << "Connected";
      }
      
      void Runnable::onDisconnect()
      {
          qDebug() << "Disconnected";
          eventLoop->exit();
      }
      
      void Runnable::onReadyRead()
      {
          parse(socket->readAll());
      }
      
      void Runnable::parse(QString httpRequest)
      {
          qDebug().noquote()   << httpRequest;
          QRegularExpression methodRegExp("\\b(GET|POST)\\b");
          QRegularExpressionMatch matchMethod = methodRegExp.match(httpRequest);
      
          if(matchMethod.hasMatch())
          {
              QString method = matchMethod.captured(0);
              if(method == "GET")
              {
      
              }
              else if(method == "POST")
              {
                  QRegularExpression uriRegExp("/(.*?)\\s");
                  QRegularExpressionMatch matchURI = uriRegExp.match(httpRequest);
      
                  QString uri = matchURI.captured(0);
                  uri.truncate(uri.lastIndexOf(QChar(' ')));
                  qDebug() << uri;
      
                  QRegularExpression bodyRegExp("(\\[.*?\\]|\\{.*?\\})");
                  QRegularExpressionMatch matchBody = bodyRegExp.match(httpRequest);
      
                  qDebug() << matchBody;
      
                  socket->write("From server: hello world");
                  socket->flush();
                  socket->waitForBytesWritten();
              }
          }
          else
          {
              qDebug() << "No match";
          }
      }
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Subuday said in QObject: cannot create children for a parent which is in a different thread:

      When I add to my code

      Add what?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

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