Quote Originally Posted by caduel View Post
where have you read that?

In Qt4 you can either:
* work with blocking I/O (not in the gui thread, as otherwise your app freezes til the I/O is over)
* work with non-blocking I/O: you need a QEventLoop running in your thread - you may use the main (i.e. gui) thread, but you do not have to.

HTH
Here is a sample code to illustrate the problem I have encountered. I made it as brief as possible. Hence, it is not going with the best practice. Please forgive me for that.
Qt Code:
  1. #include <QtNetwork/QTcpServer>
  2. #include <QtCore/QThread>
  3. #include <QtCore/QCoreApplication>
  4. #include <iostream>
  5. #include <assert.h>
  6.  
  7. class ServerThread : public QThread
  8. {
  9. Q_OBJECT
  10.  
  11. public:
  12. ServerThread(QObject *ipParent)
  13. : QThread(ipParent)
  14. , mpServer(0)
  15. {}
  16.  
  17. void run ()
  18. {
  19. mpServer = new QTcpServer();
  20. bool lbOk(connect(mpServer, SIGNAL(newConnection()),
  21. this, SLOT(onNewConnection())));
  22. assert(lbOk);
  23. mpServer->listen(QHostAddress::Any, 8888);
  24. }
  25.  
  26. private:
  27. QTcpServer* mpServer;
  28.  
  29. protected slots:
  30. void onNewConnection()
  31. {
  32. std::cout << "new connection!" << std::endl;
  33. }
  34. };
  35.  
  36. int main(int argc, char *argv[])
  37. {
  38. QCoreApplication a(argc, argv);
  39. ServerThread lrThread(0);
  40. // lrThread.start();
  41. lrThread.run();
  42. return a.exec();
  43. }
To copy to clipboard, switch view to plain text mode 

It runs fine with expected outcome which print a message every time a connection is made.

However, if the line 40 is uncommented and line 41 commented, the ServerThread::onNewConnection() method is never called with connections made.

It stops working while the QTcpServer is not created on the main thread.