Results 1 to 6 of 6

Thread: QTcpSocket parent-child constructor woes (also cross-thread talk)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2014
    Posts
    16
    Qt products
    Qt4
    Platforms
    Windows

    Default QTcpSocket parent-child constructor woes (also cross-thread talk)

    I spent about 10 hours yesterday with Qt stuff and I'm kind of exhausted, so I'm really bad at searching for answers on my own right now. If you guys could help me out, that would be appreciated.

    The basic idea is that I want to start up TCP socket communication (it reads accelerometer data from a microcontroller), and I want to start up a QGLWidget to do other stuff. The QTcpSocket starts up in it's own thread (apparently on its own) though and can't be set as the QObject child of the QGLWidget, or at least that was my conclusion based on the threading errors that popped up. Then I tried to get a basic QTcpSocket to work and it broke. I am failing here.

    What is wrong with the following code? I spits out the following runtime error:

    Qt Code:
    1. QObject: Cannot create children for a parent that is in a different thread.
    2. (Parent is in my_TCP_Q_socket_with_readyRead(*some address*), parent's thread is QThread(*some address*), current thread is QThread(*some address*)
    To copy to clipboard, switch view to plain text mode 

    For the record, I am I using QtCreator now. I figured out how to move between Visual Studio and QtCreator, so I just picked one.
    Here is main.cpp:
    Qt Code:
    1. #include <QTcpSocket>
    2. #include <QObject>
    3.  
    4. #include <iostream>
    5. using std::cout;
    6. using std::endl;
    7.  
    8.  
    9. class my_TCP_Q_socket_with_readyRead : public QObject
    10. {
    11. public:
    12. explicit my_TCP_Q_socket_with_readyRead(QObject *parent = 0) :
    13. QObject(parent)
    14. {
    15. m_socket_ptr = new QTcpSocket(this); // complains about parent-child threading
    16. //m_socket_ptr = new QTcpSocket(0); // no error (??but what is it doing? it is silent??)
    17. }
    18.  
    19.  
    20. private:
    21. Q_OBJECT
    22.  
    23. QTcpSocket *m_socket_ptr;
    24. };
    25.  
    26.  
    27. #include "main.moc"
    28.  
    29. int main(int argc, char *argv[])
    30. {
    31. my_TCP_Q_socket_with_readyRead s;
    32.  
    33. return 0;
    34. }
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong? I found that this code doesn't complain if I provide a 0 (integer zero) as the argument to the QTcpSocket, but why?


    As a related followup question, is it possible to use signals and slots to pass data from the thread that has the QTcpSocket to the thread that contains my QGLWidget? I think I can figure out how to make signals and slots work to call a function in QGLWidget once my custom QTcpSocket object is done reading data, but I don't know how to pass data from one to the other. Or am I missing the way this works? I haven't discovered how or when Qt starts threads on its own (which it clearly does considering that the error shown above specifies two threads).
    Last edited by amdreallyfast; 25th May 2014 at 17:18. Reason: updated contents; added in-code note of what is good and what is bad

Similar Threads

  1. Start a thread automatic at start
    By ralphot in forum Qt Programming
    Replies: 3
    Last Post: 10th July 2013, 16:54
  2. thread->start() does not work ?
    By ErrMania in forum Newbie
    Replies: 3
    Last Post: 20th November 2011, 12:08
  3. Replies: 3
    Last Post: 19th January 2010, 21:26
  4. Replies: 16
    Last Post: 7th October 2009, 09:17
  5. How to start a thread (inline)
    By DiamonDogX in forum Qt Programming
    Replies: 4
    Last Post: 28th May 2009, 22:53

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.