I have a class that inherits QThread designed to use a QTcpSocket to receive data from some server:

Qt Code:
  1. class ReceiveThread : public QThread
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. ReceiveThread( QHostAddress address, int port, QWidget * parent = 0 );
  7. virtual ~ReceiveThread();
  8.  
  9. protected:
  10. void run();
  11.  
  12. private:
  13. QAbstractSocket * _socket;
  14. QHostAddress _address;
  15. int _port;
  16. QTimer * _connectionAttemptTimer;
  17. };
  18.  
  19. void ReceiveThread::run()
  20. {
  21. _connectionAttemptTimer->start(); // problem 1
  22. _socket->connectToHost( _address, _port ); // problem 2
  23. exec();
  24. }
To copy to clipboard, switch view to plain text mode 

I also have a QWidget in which I want to use this class:

Qt Code:
  1. ConfigWidget::ConfigWidget( QWidget* parent ) : QWidget( parent ), _ui (new Ui_ConfigWidget())
  2. {
  3. _ui->setupUi( this );
  4.  
  5. _receiveThread = new ReceiveThread(
  6. QHostAddress( "192.168.1.2" ), 9000 );
  7.  
  8. connect( _ui->testConnectionPushButton, SIGNAL( clicked() ),
  9. this, SLOT( testConnectionPushButtonClicked() ) );
  10. }
  11.  
  12. void ConfigWidget::testConnectionPushButtonClicked()
  13. {
  14. _receiveThread->start();
  15. }
To copy to clipboard, switch view to plain text mode 

I basically get 2 errors when it executes the run() method of the QThread (marked above in the code):

QObject::startTimer: timers cannot be started from another thread
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTcpSocket(0x2255780), parent's thread is QThread(0x2160250), current thread is ReceiveThread(0x224fe90)

I kind of understand... but not really...