Results 1 to 5 of 5

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

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

    Hello,
    I am new to QT.
    In my project I have QTcpServer with multithreading. I need to implement that when server recieve something from one client(client is GUI aplication), send it to all clients except the one who sent data. Problem occurs when I call socket->write(data) in function sendToAllClients() in server.cpp. I got following:
    QObject:Cannot create children for the parent that is in different thread qt.

    Could anyone please help me with this. I was searching about on google, but whatever I tried it doesn't work.
    Thanks a lot.


    My code is below:

    mythread.cpp
    Qt Code:
    1. MyThread::MyThread(qintptr ID, QObject *parent) :
    2. QThread(parent)
    3. {
    4. this->socketDescriptor = ID;
    5. this->disconnectedSocket=-1;
    6. }
    7.  
    8. void MyThread::run()
    9. {
    10. socket = new QTcpSocket();
    11.  
    12. if(!socket->setSocketDescriptor(this->socketDescriptor))
    13. {
    14. emit error(socket->error());
    15. return;
    16. }
    17.  
    18. connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
    19. connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
    20.  
    21. exec();
    22. }
    23.  
    24. void MyThread::readyRead()
    25. {
    26. readyToRead=true;
    27.  
    28. if(readyToRead==true)
    29. emit ReadyToReadAllSockets(); //ReadyToReadAllSockets() is signal I wrote to make connection between thread and server.
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

    server.cpp

    Qt Code:
    1. Server::Server(QObject *parent) :
    2. QTcpServer(parent)
    3. {
    4. connect(this,SIGNAL(newConnection()),this,SLOT(numOfClients()));
    5.  
    6.  
    7. }
    8.  
    9. void Server::incomingConnection(qintptr socketDescriptor)
    10. {
    11. clients_list.append(socketDescriptor);
    12. MyThread *thread = new MyThread(socketDescriptor, this);
    13. threads.append(thread);
    14. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    15. connect(thread, SIGNAL(finished()), this, SLOT(clientDisconnect()));
    16. connect(thread, SIGNAL(finished()), this, SLOT(removeClientFromList()));
    17. connect(thread, SIGNAL(ReadyToReadAllSockets()), this, SLOT(sendToAllClients()));
    18.  
    19.  
    20. thread->start();
    21.  
    22. }
    23.  
    24. void Server::sendToAllClients()
    25. {
    26.  
    27. qDebug()<< "Writing in sockets....";
    28. MyThread* thread;
    29. QByteArray Data;
    30.  
    31.  
    32. for(int i=0;i<threads.size();i++)
    33. {
    34. if(threads.at(i)->getIfReady())
    35. {
    36.  
    37. thread=threads.at(i);
    38. Data = threads.at(i)->getSocket()->readAll();
    39. threads.at(i)->setReady(false);
    40.  
    41. }
    42. }
    43.  
    44. for(int i=0;i<threads.size();i++)
    45. {
    46. if(threads.at(i)!=thread)
    47. {
    48. socket=threads.at(i)->getSocket();
    49. socket->write(Data); // this is where occur error
    50.  
    51. }
    52. }
    53.  
    54. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Begginer; 16th April 2016 at 23:40.

Similar Threads

  1. Replies: 4
    Last Post: 26th January 2016, 14:25
  2. Replies: 6
    Last Post: 22nd December 2011, 21:03
  3. Replies: 3
    Last Post: 19th March 2011, 15:01
  4. Replies: 6
    Last Post: 8th July 2009, 13:24
  5. Replies: 4
    Last Post: 1st May 2009, 11:00

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.