Results 1 to 5 of 5

Thread: QTcpSocket as class member of QThread Issue

  1. #1
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question QTcpSocket as class member of QThread Issue

    All,

    In below codes, I get a QObject warning message during running:
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QNativeSocketEngine(0x9cb3f98), parent's thread is BackServer(0x9ca2640), current thread is QThread(0x9c46378)

    Qt Code:
    1. class BackServer : QThread{
    2. public:
    3. BackServer(QHostAddress ip, quint16 port, QObject * parent=0);
    4. ......
    5. public slots:
    6. void sendMsg(QString);
    7. protected:
    8. void run();
    9. private slots:
    10. void slot_newConn();
    11. ......
    12. private:
    13. QTcpServer * m_ser;
    14. QTcpSocket * m_conn;
    15. quint16 m_port;
    16. };
    17.  
    18. void BackServer::run(){
    19. m_ser=new QTcpServer;
    20. connect(m_ser, SIGNAL(newConnection()), SLOT(slot_newConn()), Qt::DirectionConnect);
    21. m_ser->listen(m_ip, m_port);
    22. exec();
    23. }
    24.  
    25. void BackServer::slot_conn(){
    26. m_conn=m_ser->nextPendingConnection();
    27. }
    28.  
    29. void BackServer::sendMsg(QString msg){
    30. if (m_conn){
    31. m_conn->write(msg.toUtf8()); // < -- issue occured point
    32. m_conn->waitForBytesWritten();
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 
    I use BackServer as a member of subclass of QMainWindow:
    Qt Code:
    1. class MainWindow{
    2. ...
    3. private slots:
    4. void slot_sendMsg();
    5. void slot_startServer();
    6.  
    7.  
    8. signal:
    9. void sendMsg(QString);
    10.  
    11. private:
    12. BackServer * m_backServer;
    13. };
    14.  
    15. class MainWindow::MainWindow(...){
    16. ...
    17. connect(ui->sendButton, SIGNAL(clicked()), SLOT(slot_sendMsg()));
    18. ...
    19. }
    20.  
    21. class MainWindow::slot_startServer(){
    22. ...
    23. m_backServer=new BackServer(ip, port);
    24. connect(this, SIGNAL(sendMsg(QString), m_backServer, SLOT(sendMsg(QString);
    25. m_backServer->start();
    26. ...
    27. }
    28.  
    29. //
    30. // whichever, the QObject create child thread occured!
    31. //
    32. class MainWindow::slot_sendMsg(){
    33. #if 0
    34. if (m_backServer && m_backServer->isRunning()){
    35. m_backServer->sendMsg(ui->lineEdit->text());
    36. }
    37. #else
    38. emit sendMsg(ui->lineEdit->text());
    39. #endif
    40. }
    To copy to clipboard, switch view to plain text mode 

    any suggestion?

    attach is my Qtcode.
    Attached Files Attached Files
    Last edited by zyangxue; 10th December 2009 at 09:49.

  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: QTcpSocket as class member of QThread Issue

    The QThread object lives in the main thread and not the thread it controls. Thus all its slots are executed in the context of the main thread. This causes objects created in them to live in the context of the main thread as well, including the QTcpSocket object created by nextPendingConnection(). Now the tcp server object lives in the context of the worker thread as it was created in the run() method of your thread class. So the two objects (server and socket) live in different threads yet the socket is a child of the server. That causes the warning. You need to make sure the socket is created in the context of the same thread as the server object.
    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:

    zyangxue (11th December 2009)

  4. #3
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QTcpSocket as class member of QThread Issue

    Thank you, wysota.

    According your remind, I had fixed the issue via QObject.moveToThread()! please see attachment!
    Qt Code:
    1. void BackServer::newConn(){
    2. m_conn=m_ser->nextPendingConnection();
    3. ...
    4. m_conn->setParent(NULL);
    5. m_conn->moveToThread(QApplication::instance()->thread());
    6. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    Yours Sincerely
    Edwin Y.X. Zeng

  5. #4
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: QTcpSocket as class member of QThread Issue

    did that fix it? I might be missing something but isn't m_conn already in QApplication::instance()->thread()?

    I thought this article was great. It's deal with database, but it is really about threads.
    http://www.linuxjournal.com/article/9602

  6. #5
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QTcpSocket as class member of QThread Issue

    To tanderson:

    Yes, I have fixed it!

    The QApplication:instance()->thread() get the main thread of application, so we can move m_conn into main thread via QObject->moveToThread(). Actually m_conn will been moved into main thread after call that.
    If you want move a object into other thread except main thread, must pass the pointer of destination thread into moveToThread().

    A object is live in background thread, if it created in that thread.
    Last edited by zyangxue; 12th December 2009 at 06:53.
    Yours Sincerely
    Edwin Y.X. Zeng

Similar Threads

  1. Error on build..QThread class
    By freekill in forum Qt Programming
    Replies: 1
    Last Post: 19th November 2009, 05:45
  2. Using a QMutex as a static class member
    By emostar in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2009, 13:48
  3. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  4. Replies: 4
    Last Post: 19th March 2008, 17:47
  5. Problem with QTcpSocket in QThread
    By Raistlin in forum Qt Programming
    Replies: 8
    Last Post: 6th October 2007, 12:23

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.