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

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 24.7k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    aekam
    wrote on last edited by
    #1

    I have a thread class in which i have a TCP socket.
    now whenever i run the application, i get a message saying "QObject: cannot create children for a parent which is in a different thread". I know it is because of the thread where i take the instance of class is different and the thread where i use the socket is different.

    my question is what problem can it cause in functionality.??

    If you take care of inches, you won't have to worry about miles... :)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      shoyeb
      wrote on last edited by
      #2

      can u give some code snippet....
      so that we can help u...

      There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tobias.hunger
        wrote on last edited by
        #3

        QObjects interact with their parents and children. For performance reasons they do so in a non-threadsafe manner.

        So your setup may result in data corruption and crashes. Better fix those messages!

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aekam
          wrote on last edited by
          #4

          @
          // HEADER FILE ----------------------------------------

          #ifndef THREADCLASS_H
          #define THREADCLASS_H

          #include <QObject>
          #include <QThread>
          #include <QtNetwork/QTcpSocket>

          class ThreadClass : public QThread
          {
          Q_OBJECT

          public:
          ThreadClass(QString serverAddress, quint16 serverPort);
          void run();

          protected:
          QString ipAddr;
          quint16 tcpPort;
          QTcpSocket tcpSocket;
          };

          #endif // THREADCLASS_H

          // SOURCE FILE ----------------------------------------

          #include "threadclass.h"

          ThreadClass::ThreadClass(QString serverAddress, quint16 serverPort)
          {
          ipAddr = serverAddress;
          tcpPort = serverPort;
          }

          void ThreadClass::run()
          {
          tcpSocket.connectToHost(ipAddr, tcpPort);
          if(tcpSocket.waitForConnected() == true)
          {
          qDebug() << "connected";
          }
          else
          {
          qDebug() << "timeout";
          }
          }

          // MAIN FILE ----------------------------------------

          #include <QtCore/QCoreApplication>
          #include "threadclass.h"

          int main(int argc, char *argv[])
          {
          QCoreApplication a(argc, argv);
          ThreadClass threadClass("www.google.com", 80);

          threadClass.start();
          
          return a.exec&#40;&#41;;
          

          }
          @

          If you take care of inches, you won't have to worry about miles... :)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shoyeb
            wrote on last edited by
            #5

            excellent answer tobias....
            this will help him...

            There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              aekam
              wrote on last edited by
              #6

              IT GAVE ME FOLLOWING RUNTIME MESSAGES...

              QObject: Cannot create children for a parent that is in a different thread.
              (Parent is QTcpSocket(0xbf9ec2bc), parent's thread is QThread(0x92ab3e0), current thread is ThreadClass(0xbf9ec2ac)
              QObject: Cannot create children for a parent that is in a different thread.
              (Parent is QTcpSocket(0xbf9ec2bc), parent's thread is QThread(0x92ab3e0), current thread is ThreadClass(0xbf9ec2ac)

              If you take care of inches, you won't have to worry about miles... :)

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tobias.hunger
                wrote on last edited by
                #7

                Check http://developer.qt.nokia.com/wiki/Threads_Events_QObjects for instructions on how to do this correctly.

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on last edited by
                  #8

                  On a sidenote: there is no need to use a seperate thread for just having asynchronous networking, as Qts networking classes already provide an non-blocking interface using signals and slots.

                  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