Qt Code:
  1. class ClientThread : public QThread
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. ClientThread(QObject *parent,int descriptor);
  7. ~ClientThread();
  8.  
  9. private:
  10. int descriptor;
  11. void run();
  12.  
  13. private slots:
  14. void onFinish();
  15. void tcpError( QAbstractSocket::SocketError error );
  16. void onStateChanged( QAbstractSocket::SocketState socketState);
  17.  
  18. };
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include "clientthread.h"
  2.  
  3. ClientThread::ClientThread(QObject *parent, int descriptor)
  4. : QThread(parent)//, socket(this)
  5. {
  6. this->descriptor=descriptor;
  7. qWarning("ClientThread::ClientThread() %d\n", QThread::currentThreadId());
  8.  
  9. connect(this,SIGNAL(finished()),this,SLOT(onFinish()));
  10.  
  11. }
  12.  
  13. ClientThread::~ClientThread()
  14. {
  15.  
  16. }
  17.  
  18. void ClientThread::run(){
  19.  
  20. QTcpSocket socket;
  21.  
  22. if( !socket.setSocketDescriptor( descriptor ) )
  23. {
  24. qDebug( "Socket error!" );
  25. return;
  26. }
  27. connect(&socket,SIGNAL( stateChanged (QAbstractSocket::SocketState)),this,SLOT (onStateChanged(QAbstractSocket::SocketState)));
  28. connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(tcpError(QAbstractSocket::SocketError)) );
  29.  
  30. QString add=socket.peerAddress().toString();
  31. qWarning()<<add;
  32.  
  33. qWarning("ClientThread::run() %d\n", QThread::currentThreadId());
  34. exec();// this will keep the thread running.
  35. }
  36.  
  37. void ClientThread::onFinish(){
  38. int a = 12;
  39.  
  40. }
  41.  
  42.  
  43.  
  44. void ClientThread::tcpError(QAbstractSocket::SocketError error){
  45.  
  46. if( error == QAbstractSocket::RemoteHostClosedError )
  47. return;
  48.  
  49. }
  50.  
  51. void ClientThread::onStateChanged( QAbstractSocket::SocketState socketState){
  52. qWarning("ClientThread::onStateChanged() %d\n", QThread::currentThreadId());
  53. switch (socketState){
  54. case QAbstractSocket::UnconnectedState:
  55. qWarning()<<"UnconnectedState";
  56. default:
  57. qWarning()<<socketState;
  58. }
  59.  
  60.  
  61. }
To copy to clipboard, switch view to plain text mode 


this two lines are not working :

connect(&socket,SIGNAL( stateChanged (QAbstractSocket::SocketState)),this,SLOT (onStateChanged(QAbstractSocket::SocketState)));
connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(tcpError(QAbstractSocket::SocketError)) );

and show error:

"QObject::connect: Cannot queue arguments of type 'QAbstractSocket::SocketError'
(Make sure 'QAbstractSocket::SocketError' is registered using qRegisterMetaType(
).)
QObject::connect: Cannot queue arguments of type 'QAbstractSocket::SocketState'
(Make sure 'QAbstractSocket::SocketState' is registered using qRegisterMetaType(
).)"

can anyone tell me how to link the signals of QtcpSocket to the slot in side of qthread???

qt is fucked