Hello, I am trying to deal with threading affinity. I think I did everything like it was suggested in the "you're doing it wrong" article, but still I receive error messages from the socket when I call waitForConnected method:
"QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTcpSocket(0x2022608), parent's thread is QThread(0x1fc8320), current thread is QThread(0x40fde8)"

Here is my code, I create controller class that is executed in the separate thread.
Qt Code:
  1. test::test(QWidget *parent, Qt::WFlags flags)
  2. : QMainWindow(parent, flags)
  3. {
  4. ui.setupUi(this);
  5.  
  6. controller = new Controller();
  7. controller->moveToThread(&thread);
  8. connect(&thread, SIGNAL(started()), controller, SLOT(start()));
  9. connect(controller, SIGNAL(finished()), &thread, SLOT(quit()));
  10.  
  11. connect(this, SIGNAL(fileAdded(const QString &)), controller, SLOT(enqueueFile(const QString &)));
  12.  
  13. emit fileAdded("lalalala");
  14.  
  15. thread.start();
  16. }
To copy to clipboard, switch view to plain text mode 

Controller class declaration:
Qt Code:
  1. #ifndef CONTROLLER_H
  2. #define CONTROLLER_H
  3.  
  4. #include <QThread>
  5. #include <QTcpSocket>
  6.  
  7. class Controller: public QObject
  8. {
  9. Q_OBJECT
  10.  
  11. public:
  12. Controller(QObject *parent = 0);
  13. ~Controller();
  14.  
  15. public slots:
  16. void enqueueFile(const QString &fileName);
  17. void start();
  18.  
  19. signals:
  20. void finished();
  21.  
  22.  
  23. private:
  24. QTcpSocket socket;
  25. };
  26.  
  27. #endif // CONTROLLER_H
To copy to clipboard, switch view to plain text mode 

Controller class implementation:
Qt Code:
  1. #include "controller.h"
  2.  
  3. #include <QDebug>
  4. #include <QTimer>
  5. #include <QEventLoop>
  6.  
  7. Controller::Controller(QObject *parent)
  8. {
  9.  
  10. }
  11.  
  12. Controller::~Controller()
  13. {
  14.  
  15. }
  16.  
  17. void Controller::start()
  18. {
  19. socket.connectToHost("some-host", 777);
  20. socket.waitForConnected(); // here goes the error
  21. socket.disconnectFromHost();
  22. socket.waitForDisconnected(); // here goes the error again
  23. }
  24.  
  25. void Controller::enqueueFile(const QString &fileName)
  26. {
  27. qDebug() << fileName;
  28. }
To copy to clipboard, switch view to plain text mode 

I know threads are not really needed for sockets, I could use signal/slot mechanism, but this is just for test. Please help me to understand what is wrong.