How to correctly move event handler on QThread
Have been using QThread for some time now but still have not understood how to correctly run event handler for thread.
Event handler is running from main/gui thread , if using code below.
How to start for each socket_thread class individual event handler?
QThread * t = new QThread; TCP_Socket * socket_thread = new TCP_Socket; socket_thread->New_socket(server->nextPendingConnection()); socket_thread->moveToThread(t); t->start(); qDebug() <<"main thr is " << QThread::currentThreadId(); Client_count++;
void TCP_Socket::New_socket(QTcpSocket* ptr)// here signals , slots defined
ConSocket = ptr;
connect(ConSocket, SIGNAL(connected()),this, SLOT(connected())); connect(ConSocket, SIGNAL(disconnected()),this, SLOT(disconnected())); connect(ConSocket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64))); connect(ConSocket, SIGNAL(readyRead()),this, SLOT(readyReadCon()));
You may want to take a look at the threaded fortune server example.
That example appears to run 1 time response to incoming connection under void run() in seperate thread but not sure if it has event handler that has signals and slots running.
Solved , it was working correct , called QThread::currentThreadId() from wrong place in code.