Hi,
I have a multithread application with 1 server and multiple client.

This is my QTCode:

Qt Code:
  1. #ifndef MESSAGETHREAD_H
  2. #define MESSAGETHREAD_H
  3.  
  4. #include <QThread>
  5. #include <QTcpSocket>
  6. #include <QTcpServer>
  7.  
  8. class MessageThread : public QThread
  9. {
  10. Q_OBJECT
  11.  
  12. public:
  13. MessageThread();
  14.  
  15. void run();
  16.  
  17. public slots:
  18. void newMessConnection();
  19.  
  20. signals:
  21. void error(QTcpSocket::SocketError socketError);
  22.  
  23. private:
  24. QTcpServer* messageSrv;
  25. QString text;
  26. };
  27.  
  28. #endif
To copy to clipboard, switch view to plain text mode 

Implementation:

Qt Code:
  1. #include "MessageThread.h"
  2.  
  3. #include <QtNetwork>
  4.  
  5.  
  6. #define IPADDRESS "192.168.1.160"
  7.  
  8. MessageThread::MessageThread()
  9. {
  10. }
  11.  
  12. void MessageThread::run()
  13. {
  14. QTcpServer* messageServer = new QTcpServer;
  15. messageSrv = messageServer;
  16. connect(messageServer, SIGNAL(newConnection()), this, SLOT(newMessConnection()));
  17.  
  18. QHostAddress xIpAddress;
  19. quint16 port;
  20.  
  21. xIpAddress.setAddress(IPADDRESS);
  22. port = 2223;
  23.  
  24. if(!messageServer->listen(xIpAddress, port))
  25. {
  26. printf("\n Communication Manager.d: Unable to start the server.");
  27. messageServer->close();
  28. }
  29.  
  30. if (messageServer->isListening())
  31. {
  32. printf("\n Message Server initialized");
  33. }
  34. exec();
  35.  
  36.  
  37. }
  38.  
  39. void MessageThread::newMessConnection()
  40. {
  41. printf("\n Message server: send data....");
  42. QTcpSocket* connection = messageSrv->nextPendingConnection();
  43. QString test = "test data";
  44.  
  45. QByteArray block;
  46. QDataStream out(&block, QIODevice::WriteOnly);
  47. out.setVersion(QDataStream::Qt_4_0);
  48. out << (quint16)0;
  49. out << test;
  50. out.device()->seek(0);
  51. out << (quint16)(block.size() - sizeof(quint16));
  52. connection->write(block);
  53.  
  54. }
To copy to clipboard, switch view to plain text mode 


When I create this thread the line "connection->write(block);" show me this error:

QObject: Cannot create children for a parent that is in a different thread.
Parent's thread is MessageThread, current thread is QThread.


How i can solve it?
Can anyone help me please?

Bye