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 that is in a different thread.

QObject: Cannot create children for a parent that is in a different thread.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 21.6k 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.
  • mandruk1331M Offline
    mandruk1331M Offline
    mandruk1331
    wrote on last edited by
    #1

    I want to open the listen method in a different thread, it shows that it's listening but I can't connect to the server side not from client nor from telnet. I googled and found that I have to use SLOT/SIGNAL mechanism but don't understand use it for what?

    Start Thread for listening:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        for(int i=0;i<4;i++){
            socketC = new TCPSocket;
            connect(this,SIGNAL(startThread()),socketC,SLOT(StartThread()));
           // connect(socketC,SIGNAL(sendImage(QImage&)),this,SLOT(recImg(QImage&)));
            connect(socketC,SIGNAL(msg(QString)),this,SLOT(Log(QString)));
            connect(socketC,SIGNAL(users(QString)),this,SLOT(Users(QString)));
            connect(socketC,SIGNAL(idDisconnected(int)),SLOT(UpdateList(int)));
            port_struct = new PORT_;
            port_struct->locked = true;
            socketC->setPORT(DEFAULT_PORT+i);
            socketC->setId(i);
            SocketInst.push_back(socketC);
            PORTS_NUM.push_back(port_struct);
            //SocketInst.at(i)->start();
            emit startThread();
        }
    }
    

    inside the thread function:

    TCPSocket::TCPSocket()
    {
        socket = new QTcpSocket;
        connect(&server,SIGNAL(newConnection()),this,SLOT(newConnectionS()));
        connect(socket,SIGNAL(disconnected()),this,SLOT(Disconnected()));
        connect(&timer_,SIGNAL(timeout()),this,SLOT(Disconnected()));
        emit msg("Itializing server side...");
    }
    void TCPSocket::run(){
        if(server.isListening() ){
    
        }else{
            this->ListenS();
        }
    }
    
    J.HilkJ 1 Reply Last reply
    0
    • mandruk1331M mandruk1331

      I want to open the listen method in a different thread, it shows that it's listening but I can't connect to the server side not from client nor from telnet. I googled and found that I have to use SLOT/SIGNAL mechanism but don't understand use it for what?

      Start Thread for listening:

      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          for(int i=0;i<4;i++){
              socketC = new TCPSocket;
              connect(this,SIGNAL(startThread()),socketC,SLOT(StartThread()));
             // connect(socketC,SIGNAL(sendImage(QImage&)),this,SLOT(recImg(QImage&)));
              connect(socketC,SIGNAL(msg(QString)),this,SLOT(Log(QString)));
              connect(socketC,SIGNAL(users(QString)),this,SLOT(Users(QString)));
              connect(socketC,SIGNAL(idDisconnected(int)),SLOT(UpdateList(int)));
              port_struct = new PORT_;
              port_struct->locked = true;
              socketC->setPORT(DEFAULT_PORT+i);
              socketC->setId(i);
              SocketInst.push_back(socketC);
              PORTS_NUM.push_back(port_struct);
              //SocketInst.at(i)->start();
              emit startThread();
          }
      }
      

      inside the thread function:

      TCPSocket::TCPSocket()
      {
          socket = new QTcpSocket;
          connect(&server,SIGNAL(newConnection()),this,SLOT(newConnectionS()));
          connect(socket,SIGNAL(disconnected()),this,SLOT(Disconnected()));
          connect(&timer_,SIGNAL(timeout()),this,SLOT(Disconnected()));
          emit msg("Itializing server side...");
      }
      void TCPSocket::run(){
          if(server.isListening() ){
      
          }else{
              this->ListenS();
          }
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #2

      @mandruk1331 hi,
      first of, let me show you think link to the wiki page its quite useful for Qt and threading.

      to your problem,
      you create your socket object in the constructor of your class

      TCPSocket::TCPSocket()
      {
          socket = new QTcpSocket;
      

      that means the object is created here in the main thread:

      for(int i=0;i<4;i++){
              socketC = new TCPSocket;
      

      thats were your error msg is from.

      to fix that move the socket creation in your run function:

      void TCPSocket::run(){
          if(start){
              socket = new QTcpSocket;
              start = false;
          }
          if(server.isListening() ){
      
          }else{
              this->ListenS();
          }
      }
      

      but I would still suggest the worker abroach described in the wiki page. Than overwriting run of your Qthread class.

      edit: fixed format issue.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      mandruk1331M 1 Reply Last reply
      1
      • J.HilkJ J.Hilk

        @mandruk1331 hi,
        first of, let me show you think link to the wiki page its quite useful for Qt and threading.

        to your problem,
        you create your socket object in the constructor of your class

        TCPSocket::TCPSocket()
        {
            socket = new QTcpSocket;
        

        that means the object is created here in the main thread:

        for(int i=0;i<4;i++){
                socketC = new TCPSocket;
        

        thats were your error msg is from.

        to fix that move the socket creation in your run function:

        void TCPSocket::run(){
            if(start){
                socket = new QTcpSocket;
                start = false;
            }
            if(server.isListening() ){
        
            }else{
                this->ListenS();
            }
        }
        

        but I would still suggest the worker abroach described in the wiki page. Than overwriting run of your Qthread class.

        edit: fixed format issue.

        mandruk1331M Offline
        mandruk1331M Offline
        mandruk1331
        wrote on last edited by mandruk1331
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • mzimmersM mzimmers referenced this topic on

        • Login

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