Hi,

Qt4.2.2 on linux.
I have implemented a threaded communication scheme, but the slots of a non gui thread class are executed in the GUI thread.
I was hoping maybe some one here could point to my mistake.
Here is what I did in meta code, after the code a text explaining the behavior:
Qt Code:
  1. //Base socket thread class
  2. class SocketThread : public QThread
  3. {
  4. Q_OBJECT
  5. protected:
  6. QTcpSocket *m_socket;
  7. QString m_host;
  8. quint32 m_port;
  9. QMutex m_socketMutex;
  10. QDataStream *m_dataStream;
  11. public:
  12. SocketThread(QString host,quint32 port,QObject *parent = 0);
  13.  
  14. ~SocketThread();
  15. const QDataStream & stream();
  16. void run();
  17. public slots:
  18. virtual void readSocket();
  19. };
  20.  
  21. void SocketThread::run()
  22. {
  23. if(!m_socket)
  24. {
  25. m_socket = new QTcpSocket();
  26. if(m_socket) m_dataStream = new QDataStream(m_socket);
  27. connect(m_socket,SIGNAL(readyRead()),this,SLOT(readSocket()));
  28. if(m_socket) m_socket->connectToHost(m_host,m_port);
  29. }
  30. exec();
  31. }
  32.  
  33. //Actual working class
  34. class HPSCSocket : public SocketThread
  35. {
  36. Q_OBJECT
  37. public:
  38. HPSCSocket(QString host,quint32 port,QObject *parent = 0);
  39.  
  40. ~HPSCSocket();
  41. signals:
  42. void sig_hpscReadout(int,float,float);
  43. public slots:
  44. void sendHPSCCommand(HPSC_Command cmd,long usWait);
  45. void readSocket();
  46.  
  47. };
  48.  
  49. void HPSCSocket::sendHPSCCommand(HPSC_Command cmd,long usWait=0)
  50. {
  51. qDebug()<<"HPSCSocket::sendHPSCCommand";
  52. if(m_dataStream)
  53. {
  54. qDebug()<<"sending command";
  55. *m_dataStream<<cmd;
  56. m_socket->flush();
  57. qDebug()<<"about to sleep "<<usWait;
  58. usleep(usWait);
  59. qDebug()<<"woke up";
  60. }
  61. }
  62.  
  63. //the class (GUI) that communicates through the threaded socket class HPSCSocket
  64. FrmPower::FrmPower(BUS bus1,BUS bus2,QWidget *parent)
  65. : QWidget(parent)
  66. {
  67. m_hpscSocket = new HPSCSocket(m_hpscHost.toString(),m_hpscPort,this);
  68. connect(this,SIGNAL(sig_hpscCommand(HPSC_Command,long)),m_hpscSocket,SLOT(sendHPSCCommand(HPSC_Command,long)),Qt::QueuedConnection);
  69. connect(m_hpscSocket,SIGNAL(sig_hpscReadout(int,float,float)),this,SLOT(hpscReadout(int,float,float)));
  70. }
  71.  
  72. void FrmPower::bus1()
  73. {
  74. command.cmd = SETCURR1;
  75. command.var = m_src1a;
  76. emit sig_hpscCommand(command,120000);
  77. }
To copy to clipboard, switch view to plain text mode 

As you can see, a socket is created in its own thread (not the GUI thead) in SocketThread::run().
The GUI class FrmPower has a member variable of type HPSCSocket which is a subclass of the SocketThread class.
The aim of this scheme, is to allow FrmPower to send information through the threaded socket as can be seen in FrmPower::bus1().
(The reason is, that after each socket transmition there is a need for various sleep intervals, which would freeze the GUI thread, and other problems as well)
The problem is, that although the socket is created in seperate thread to the GUI thread, the execution of HPSCSocket::sendHPSCCommand() happens in the main GUI thread, and I have no idea why.
In addition, the output:
QSocketNotifier: socket notifiers cannot be enabled from another thread
comes out in HPSCSocket::sendHPSCCommand on the socket operations, but code runs, just not in the right thread.
If any one could point me to my mistake it will be very appreciated.
I didn't insert code that is not relevant for illustrating the problem, such as proper constructors , mutextes etc.

Thanks in advance!