Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How moveToThread() works?



  • can it get called by itself in any way ?
    After I move my object lets call it worker to thread, I connect signals from serverclass to the slots in worker class.
    incommingconnection(qintptr sd){

        QThread *mythread=new QThread();
       WorkerS *worker=new WorkerS(sd);
    
        worker->moveToThread(mythread);
    

    //from here some connections
    emit signal();//this signal too is connected to the slot in worker class
    }

    now in worker class the slot which gets connected to the signal emmited is

    void worker::connectionSocket(sd){

    QTcpSocket *m_socket=new QTcpSocket();
    m_socket->setSocketDescriptor(s)
    }
    so this slot:: connectionSocket(sd) gets executed (number of socket already connected *2)+1 times
    and I really can't figure out why



  • @MokJ said in How moveToThread() works?:

    QTcpSocket *m_socket=

    Very strange to have a local variable with m_ prefix... could you post your entire code?



  • @VRonin
    void server::startserver(){
    it does the starting server things
    }
    incommingconnection(qintptr sd){

    QThread *mythread=new QThread();
    

    WorkerS *worker=new WorkerS(sd);

    worker->moveToThread(mythread);
    

    //from here some connections
    emit connectTheClients();
    }

    worker::worker(qintptr sd,QObject*parent):QObject(parent){
    s=sd;
    }
    void worker::connectionSocket(){

    QTcpSocket *socket=new QTcpSocket();
    socket->setSocketDescriptor(s)
    }

    m_socket , its just a name i've given



  • Could you post the header file for worker?

    P.S.
    you can use 3 consecutive ` to mark the beginning and the end of a code block, it helps a lot with readability here



  • thanks @VRonin , I've debugged the code and now its working as it should.
    Thanks again @VRonin .


Log in to reply