Results 1 to 7 of 7

Thread: "Cannot create children for a parent that is in a different thread"

  1. #1
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default "Cannot create children for a parent that is in a different thread"

    I have a class that inherits QThread designed to use a QTcpSocket to receive data from some server:

    Qt Code:
    1. class ReceiveThread : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ReceiveThread( QHostAddress address, int port, QWidget * parent = 0 );
    7. virtual ~ReceiveThread();
    8.  
    9. protected:
    10. void run();
    11.  
    12. private:
    13. QAbstractSocket * _socket;
    14. QHostAddress _address;
    15. int _port;
    16. QTimer * _connectionAttemptTimer;
    17. };
    18.  
    19. void ReceiveThread::run()
    20. {
    21. _connectionAttemptTimer->start(); // problem 1
    22. _socket->connectToHost( _address, _port ); // problem 2
    23. exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

    I also have a QWidget in which I want to use this class:

    Qt Code:
    1. ConfigWidget::ConfigWidget( QWidget* parent ) : QWidget( parent ), _ui (new Ui_ConfigWidget())
    2. {
    3. _ui->setupUi( this );
    4.  
    5. _receiveThread = new ReceiveThread(
    6. QHostAddress( "192.168.1.2" ), 9000 );
    7.  
    8. connect( _ui->testConnectionPushButton, SIGNAL( clicked() ),
    9. this, SLOT( testConnectionPushButtonClicked() ) );
    10. }
    11.  
    12. void ConfigWidget::testConnectionPushButtonClicked()
    13. {
    14. _receiveThread->start();
    15. }
    To copy to clipboard, switch view to plain text mode 

    I basically get 2 errors when it executes the run() method of the QThread (marked above in the code):

    QObject::startTimer: timers cannot be started from another thread
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QTcpSocket(0x2255780), parent's thread is QThread(0x2160250), current thread is ReceiveThread(0x224fe90)

    I kind of understand... but not really...

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: "Cannot create children for a parent that is in a different thread"

    Note that the QThread object (and all its member variables and all things created in your (subclass's) constructor belong to the thread the QThread object is created in.

    Thus you get a "conflict" when you try to use those things from "your" thread.
    One way to work around that is to created needed resources at the beginning of your thread's run() function.

    be sure to read threads
    HTH

  3. #3
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: "Cannot create children for a parent that is in a different thread"

    I have read about threads on the link you sent.

    I understand the timer thing, so I tired actually instantiating it in the run() method, which worked fine, but a problem I ran into was in my slot for its timeout() signal, in which I wish to call stop() on the timer. I get:

    "QObject::killTimer: timers cannot be stopped from another thread"

    -----------------------
    EDIT: Hmm I actually got rid of that error by using "Qt::ConnectionType:: DirectConnection" in the connect call.
    Last edited by DiamonDogX; 7th July 2009 at 22:41.

  4. #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: "Cannot create children for a parent that is in a different thread"

    Of course you know that QTcpSocket works in asynchronous manner and you don't need a dedicated thread to use it, right?
    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.


  5. #5
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: "Cannot create children for a parent that is in a different thread"

    Yes but I would prefer to encapsulate a lot of things in my thread class besides the QTcpSocket...

  6. #6
    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: "Cannot create children for a parent that is in a different thread"

    As for now you are making things harder than they could be. Especially that eventually you're running an event loop in the other thread so the only difference between this application and the application where you'd be doing everything in one thread is that... you have two threads. And the application becomes slower (with two threads instead of one) but let's ignore that for now.
    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.


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

    bob2oneil (22nd March 2011)

  8. #7
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: "Cannot create children for a parent that is in a different thread"

    I will definitely keep that in mind, thanks.

Similar Threads

  1. How to Create child widget behind parent widget?
    By anupamgee in forum Qt Programming
    Replies: 6
    Last Post: 22nd June 2010, 14:03
  2. Replies: 4
    Last Post: 1st May 2009, 12:00
  3. Replies: 2
    Last Post: 19th August 2008, 10:46
  4. Replies: 3
    Last Post: 29th May 2008, 14:50

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.