Results 1 to 4 of 4

Thread: tcpSocket and threads: Cannot create children for a parent that is in a different thr

  1. #1
    Join Date
    Feb 2011
    Posts
    31
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Post tcpSocket and threads: Cannot create children for a parent that is in a different thr

    hi
    using visual studio 2008 and Qt 4.7.1, i am facing the weird warning from QOBJECT "Cannot create children for a parent that is in a different thread.". i cannot afford to understand this explainaition :-) athttp://cep.xor.aps.anl.gov/software/qt4-x11-4.2.2-browser/dc/d4f/class_q_object.html so here i am :-)

    i created a thread which redefines the runmerìthod as stated in the documentation.
    major steps are

    1. I create the QTcp socket in the new thread class constructor nd connect the relavant signals. the network connection has already been created by the main application

    Qt Code:
    1. queueProg::queueProg(QString ipAddress, quint16 ipPort, quint8 dutNumber, tOpCode opCode)
    2. {
    3. ......
    4. this->tcpSocket = new QTcpSocket(this);
    5. connect(tcpSocket, SIGNAL(connected()), this, SLOT(sendMsg()));
    6. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readMsg()));
    7. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
    8. this, SLOT(handleSocketError(QAbstractSocket::SocketError)));
    To copy to clipboard, switch view to plain text mode 

    2. overload the run method as required

    Qt Code:
    1. void queueProg::run()
    2. {
    3. this->tcpSocket->abort(); // just reset the connection
    4. this->tcpSocket->connectToHost(this->ipAddress, this->ipPort);
    5. exec();
    6. return;
    7. }
    To copy to clipboard, switch view to plain text mode 

    3. create TWO class object of this thread which open TCP sockets at the same address but different ports

    Qt Code:
    1. queueProg *picQueueProg =
    2. new queueProg(this->ipAddress, this->ipPort, this->dutNr, PICPROG);
    3. queueProg *mceQueueProg =
    4. new queueProg(this->ipAddress, this->ipPort+1, this->dutNr, MCEPROG);
    To copy to clipboard, switch view to plain text mode 


    There is no black magic: i get this strategy by looking at various example in the web: it could be i probably mixed them wrongly :-)

    maybe the tcpSocket is silently associated to another thread when creatd a new tcpsocket object ...

    more runtime errors here

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QTcpSocket(0xbd9d90), parent's thread is QThread(0xa88ce0), current thread is queueProg(0xa89180)


    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QTcpSocket(0xbd9d90), parent's thread is QThread(0xa88ce0), current thread is queueProg(0xa89180)

    what about debuggin threads? can debug mode be safely used or is too much invasive?

    thanks much for your help
    Last edited by wysota; 18th March 2011 at 20:24. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: tcpSocket and threads: Cannot create children for a parent that is in a different

    Quote Originally Posted by marco.stanzani View Post
    hi
    using visual studio 2008 and Qt 4.7.1, i am facing the weird warning from QOBJECT "Cannot create children for a parent that is in a different thread.". i cannot afford to understand this explainaition :-)
    So, what's weird in this message?

    i created a thread which redefines the runmerìthod as stated in the documentation.
    Unfortunately the documentation is wrong in encouraging you to reimplement run().
    There is no black magic: i get this strategy by looking at various example in the web: it could be i probably mixed them wrongly :-)
    The examples are also wrong.

    maybe the tcpSocket is silently associated to another thread when creatd a new tcpsocket object ...
    It's not associated with anything silently. You have two objects that live in different threads and you try to make one parent of the other.

    what about debuggin threads? can debug mode be safely used or is too much invasive?
    Yes, that's not a problem.

    Basically my suggestion is to do networking without threading. If you insist on using threads for this purpose, read this article: http://labs.qt.nokia.com/2010/06/17/...oing-it-wrong/
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    marco.stanzani (19th March 2011)

  4. #3
    Join Date
    Feb 2011
    Posts
    31
    Thanks
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: tcpSocket and threads: Cannot create children for a parent that is in a different

    thanks wysota and sorry for not quoting the C++ code with the code tag

    thanks also for the interesting link (BTW the qt official documentation advise to overload the run method when thrading so here i was ... i think there is room for fixing it)

    you advise not using threads and networking which sounds bad to me since i i am designing a client communicating using two IP ports wit the server and each IP port is related to different task demanded to the server. in order to maximize the execution speed i need two tcpSocket opened at the conencted IP address and different ports which dialogue with the server in parallel
    I cannot afford that socket1 act first, socket2 wait for completion fo socket2, so i wAS thinking about threads ...
    maybe i can simply create two different sockets in my application and connect the usual signals to different sendMessage and readMessage slots ...

    have a nice weekend

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: tcpSocket and threads: Cannot create children for a parent that is in a different

    Quote Originally Posted by marco.stanzani View Post
    thanks also for the interesting link (BTW the qt official documentation advise to overload the run method when thrading so here i was ... i think there is room for fixing it)
    That's true, the docs are old in this regard and need fixing.

    you advise not using threads and networking which sounds bad to me since i i am designing a client communicating using two IP ports wit the server and each IP port is related to different task demanded to the server. in order to maximize the execution speed i need two tcpSocket opened at the conencted IP address and different ports which dialogue with the server in parallel
    I cannot afford that socket1 act first, socket2 wait for completion fo socket2, so i wAS thinking about threads ...
    Threads make your program slower not faster. It's faster to check whether there is some data waiting in any of the two sockets then to check twice if there is some data waiting in one socket. Overhead of reading the data is neglectible (unless you have a 10Gbps link that you satitate in almost 100%), it's processing the data that can take time and you can do it happily using threads.

    maybe i can simply create two different sockets in my application and connect the usual signals to different sendMessage and readMessage slots ...
    You can do whatever you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 14th April 2010, 16:03
  2. disable a parent and children in a tree model
    By qt_gotcha in forum Newbie
    Replies: 4
    Last Post: 9th July 2009, 07:49
  3. Replies: 6
    Last Post: 8th July 2009, 14:24
  4. Replies: 4
    Last Post: 1st May 2009, 12:00
  5. Replies: 3
    Last Post: 29th May 2008, 14:50

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.