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?

    ´´´

    void TCP_Server_File::NewConnection()
    {

    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()));
    

    }

    ´´´


  • Lifetime Qt Champion

    Hi,

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.