0

I'm trying to make QTcpServer start a separate thread for each connection. There's example code available that does just that: http://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html

This code works by subclassing QThread. If I build my code based on that example, I'm pretty soon running into "QObject: Cannot create children for a parent that is in a different thread." warnings.

There is documentation from Qt that advises against subclassing QThread, because that is "doing it wrong": http://blog.qt.io/blog/2010/06/17/youre-doing-it-wrong/

So, my question is, if the Qt provided example code is directly in violation of the Qt recommended practice, is there an example of the correct way to do it? Any example I've found so far subclasses QThread.

Philipp
  • 957
  • 1
  • 6
  • 20
  • i do not see any reason to move a `QTcpServer` object in another thread. it is implemented that it runs its connection in the back and emits signals when something happens. example code can be found at http://doc.qt.io/qt-5/qthread.html , so having the server run in the main thread and its 'worker' handles the events in the background should also do the job – Zaiborg Mar 17 '15 at 08:04
  • that is exactly what I'm trying to do. The "accept" part happens on the main event loop and thus the main thread. Just when a connection is established, the TCP I/O on the respective socket should happen on it's own thread. – Philipp Mar 17 '15 at 08:11
  • can you show us what your code actually looks like? – Zaiborg Mar 17 '15 at 08:22

1 Answers1

0

Qt is a developing product. And, I seem, subclassing QThread is still considered as legal practice. It is just an older technique, so old examples use it.

Your problem seems to be the wrong usage of QObject(s) across different threads, rather than QThread subclassing. First try to find the code which issues the warnings.

Matt
  • 13,674
  • 1
  • 18
  • 27