Results 1 to 4 of 4

Thread: parent is in different QThread?

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default parent is in different QThread?

    Hello!

    I have a small issue, obviously with a QThread. It's about a very simple UDP server that periodically sends out a test string. It does the job, but when I start it, it gives me a an error message and I don't understand what's wrong. Can anyone please explain me? Here is the code:

    Qt Code:
    1. #include <QtCore>
    2. #include <QCoreApplication>
    3. #include "UDPTestServer.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8.  
    9. UDPTestServer udp;
    10. udp.start();
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QThread>
    2. #include <QUdpSocket>
    3.  
    4. class UDPTestServer : public QThread
    5. {
    6. public:
    7. UDPTestServer();
    8. virtual ~UDPTestServer();
    9. void run();
    10.  
    11. private:
    12. QUdpSocket udpSocket;
    13. };
    14.  
    15. void UDPTestServer::run()
    16. {
    17. while (true)
    18. {
    19. QByteArray datagram = "Broadcast message";
    20. int bytes = udpSocket.writeDatagram(datagram.data(), datagram.size(), QHostAddress::LocalHost, 7755);
    21. sleep(1);
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    I can compile and start it and it does start sending out the "Boradcast message". However, it also prints the error message:

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QUdpSocket(0x22ff48), parent's thread is QThread(0x3e3988), current thread is QThread(0x22ff40)
    What does it exactly mean?

    I tried moving the call to start() the UDP thread to the constructor of UDPTestServer, but that doesn't make a difference.

    Thanks
    Cruz

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: parent is in different QThread?

    1. what is an implementation of UDPTestServer::UDPTestServer() ?
    2. try to do:
    Qt Code:
    1. UDPTestServer::UDPTestServer()
    2. {
    3. moveToThread( this );
    4. }
    To copy to clipboard, switch view to plain text mode 
    3. try to create QUdpSocket by pointer. e.g.
    Qt Code:
    1. QUdpSocket * udpSocket;
    2. ...
    3. UDPTestServer::UDPTestServer()
    4. {
    5. moveToThread( this );
    6. udpSocket = new QUdpSocket();
    7. }
    8. UDPTestServer::~UDPTestServer()
    9. {
    10. delete udpSocket;
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: parent is in different QThread?

    My implementation of the constructor is currently empty.

    Unfortunately, none of the proposed methods changes anything. I still see the warning when I start the program.

    What did work was when I moved the QUdpSocket from being a member of the UDPTestServer class to be a local variable in the run() method of the UDPTestServer. I can see how the constructor of UDPTestServer is executed in the main thread, while the run() method is on UDPTestserver's own thread. So basicly the UDP socket was created in a different thread than where the writeDatagram method is called. But it's still very unclear to me at what point I tried to create children of a parent. And also, why is it not ok to create the UDP socket in one thread and call writeDatagram in another?

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: parent is in different QThread?

    Make your:
    Qt Code:
    1. QUdpSocket udpSocket;
    To copy to clipboard, switch view to plain text mode 
    a local variable in UDPTestServer::run() instead of member variable. The code is "in new thread" only in run() method implementation so your udpSocket variable belongs to the main thread, and probably it wants to create some objects in writeDatagram() which is called in new thread (different then udpSocket thread).
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. The following 2 users say thank you to faldzip for this useful post:

    Cruz (6th April 2010), Threepwood (22nd February 2011)

Similar Threads

  1. Qwt Legend Parent
    By BlackFoXX in forum Qwt
    Replies: 1
    Last Post: 21st April 2009, 07:10
  2. TabWidget's Tab's parent
    By alisami in forum Qt Programming
    Replies: 12
    Last Post: 31st March 2009, 13:30
  3. Replies: 4
    Last Post: 26th June 2008, 18:41
  4. Spawn a QThread in another QThread
    By david.corinex in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2007, 12:54
  5. Using QAbstractTableModel.parent
    By Max Yaffe in forum Newbie
    Replies: 7
    Last Post: 15th June 2007, 15:21

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.