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. qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread
QtWS25 Last Chance

qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 2.9k 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.
  • R rifky

    Hi ,

    anyone can help me about qtcpsocket with qthread

    Application logs:

    Connected client  16
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QTcpSocket(0x7fce4ed06720), parent's thread is QThread(0x7fce4ed02520), current thread is UpdataServerThread(0x7fce4ed82f00)
    connected_client_socket =>  QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720)))
    connected_client_ip =>  QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720)))
    QHostAddress("::ffff:127.0.0.1") in :  asd
    
    connected_client_socket =>  QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720)))
    connected_client_ip =>  QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720)))
    QHostAddress("::ffff:127.0.0.1") in :  asd
    
    connected_client_socket =>  QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720)))
    connected_client_ip =>  QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720)))
    QHostAddress("::ffff:127.0.0.1") in :  asd
    
    connected_client_socket =>  QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720)))
    connected_client_ip =>  QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720)))
    QHostAddress("::ffff:127.0.0.1") in :  asd
    
    connected_client_socket =>  QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720)))
    connected_client_ip =>  QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720)))
    QHostAddress("::ffff:127.0.0.1") in :  sad
    
    connected_client_socket =>  QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720)))
    connected_client_ip =>  QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720)))
    QHostAddress("::ffff:127.0.0.1") in :  sad
    
    Connected client  19
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QTcpSocket(0x7fce4eea12f0), parent's thread is QThread(0x7fce4ed02520), current thread is UpdataServerThread(0x7fce4ee137e0)
    connected_client_socket =>  QHash((QHostAddress("::ffff:192.168.200.36"), QTcpSocket(0x7fce4eea12f0)))
    connected_client_ip =>  QHash((QHostAddress("::ffff:192.168.200.36"), QTcpSocket(0x7fce4eea12f0)))
    QHostAddress("::ffff:192.168.200.36") in :  asdasdas
    
    connected_client_socket =>  QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720)))
    connected_client_ip =>  QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720)))
    QHostAddress("::ffff:127.0.0.1") in :  asd
    

    what is wrong about this ?
    my code

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #2

    @rifky said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:

    Connected client 19
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QTcpSocket(0x7fce4eea12f0), parent's thread is QThread(0x7fce4ed02520), current thread is UpdataServerThread(0x7fce4ee137e0)

    What information are you looking for which is not clear from this error message?

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #3

      You should take a look at QTcpServer::incomingConnection(qintptr)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Hi and welcome to devnet,

        You are either moving a socket to a different thread or using it directly in that different thread.

        In both case it's the wrong thing to do.

        Follow @Christian-Ehrlicher advice and move the socket creation in that other thread.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        R 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          You are either moving a socket to a different thread or using it directly in that different thread.

          In both case it's the wrong thing to do.

          Follow @Christian-Ehrlicher advice and move the socket creation in that other thread.

          R Offline
          R Offline
          rifky
          wrote on last edited by rifky
          #5

          @SGaist said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:

          Hi and welcome to devnet,

          You are either moving a socket to a different thread or using it directly in that different thread.

          In both case it's the wrong thing to do.

          Follow @Christian-Ehrlicher advice and move the socket creation in that other thread.

          thx for response,
          i followed this ,i little bit confused how to implement QThread with qtcpsocket,

          can you give me different concept? aside from

          server start
          server is listening
          client connected
          thread start
          

          before this i made without thread but when two device is connected and server can receive data from them, but I found anomalies , server can't handle both of them, looks like race condition on readyRead() function.

          i guessing with qthread can solve that problem.

          JonBJ 1 Reply Last reply
          0
          • R rifky

            @SGaist said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:

            Hi and welcome to devnet,

            You are either moving a socket to a different thread or using it directly in that different thread.

            In both case it's the wrong thing to do.

            Follow @Christian-Ehrlicher advice and move the socket creation in that other thread.

            thx for response,
            i followed this ,i little bit confused how to implement QThread with qtcpsocket,

            can you give me different concept? aside from

            server start
            server is listening
            client connected
            thread start
            

            before this i made without thread but when two device is connected and server can receive data from them, but I found anomalies , server can't handle both of them, looks like race condition on readyRead() function.

            i guessing with qthread can solve that problem.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #6

            @rifky said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:

            but I found anomalies , server can't handle both of them, looks like race condition on readyRead() function.

            You should rather sort this out.

            i guessing with qthread can solve that problem.

            I'm guessing that is more likely to create problems than solve them!

            Though I may be wrong. If you do want to use threads have you looked at the example https://doc.qt.io/qt-5/qtnetwork-blockingfortuneclient-example.html ? But if it were me I would look at the non-blocking https://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html. There are no threads there, and it seems much simpler to me. Unless @Christian-Ehrlicher or @SGaist say otherwise....

            R 1 Reply Last reply
            2
            • JonBJ JonB

              @rifky said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:

              but I found anomalies , server can't handle both of them, looks like race condition on readyRead() function.

              You should rather sort this out.

              i guessing with qthread can solve that problem.

              I'm guessing that is more likely to create problems than solve them!

              Though I may be wrong. If you do want to use threads have you looked at the example https://doc.qt.io/qt-5/qtnetwork-blockingfortuneclient-example.html ? But if it were me I would look at the non-blocking https://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html. There are no threads there, and it seems much simpler to me. Unless @Christian-Ehrlicher or @SGaist say otherwise....

              R Offline
              R Offline
              rifky
              wrote on last edited by rifky
              #7

              @JonB said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:

              Though I may be wrong. If you do want to use threads have you looked at the example https://doc.qt.io/qt-5/qtnetwork-blockingfortuneclient-example.html ? But if it were me I would look at the non-blocking https://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html. There are no threads there, and it seems much simpler to me. Unless @Christian-Ehrlicher or @SGaist say otherwise....

              I've tried that, i made it into support with multi clients, i put peerAddress with socket pointer in qHash,

              before this i made without thread but when two device is connected and server can receive data from them, but I found anomalies , server can't handle both of them, looks like race condition on readyRead() function.

              but in this case different with this topic .

              anyone can help me to fix my code ?

              1 Reply Last reply
              0
              • Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @rifky said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:

                anyone can help me to fix my code?

                which code?

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                JonBJ 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @rifky said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:

                  anyone can help me to fix my code?

                  which code?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #9

                  @Christian-Ehrlicher There is a link to a zip file download at the end of his first post :)

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    You have to create the socket in the run() function as only there the new thread is running. See https://doc.qt.io/qt-5/qthread.html#details

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    R 1 Reply Last reply
                    1
                    • Christian EhrlicherC Christian Ehrlicher

                      You have to create the socket in the run() function as only there the new thread is running. See https://doc.qt.io/qt-5/qthread.html#details

                      R Offline
                      R Offline
                      rifky
                      wrote on last edited by rifky
                      #11

                      @Christian-Ehrlicher said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:

                      You have to create the socket in the run() function as only there the new thread is running. See https://doc.qt.io/qt-5/qthread.html#details

                      its solved , i have tried to create socket in run() function

                      void UpdataServerThread::run()
                      {
                          updata_socket = new QTcpSocket();
                          if (!updata_socket->setSocketDescriptor(socketDescriptor)) {
                              emit error(updata_socket->error());
                              return;
                          }
                      
                          connect(updata_socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::QueuedConnection);
                          connect(updata_socket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection);
                      
                          connected_client_socket.insert(updata_socket->peerAddress(),updata_socket);
                          qDebug()<<"creating socket ptr";
                      
                          updata_socket->write("Welecome Message");
                          
                      
                          exec();
                      }
                      

                      Thanks alot sir ,, i got new another problem with sending/write data on socket maybe i shoud make new topic for this

                      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