Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to correctly move event handler on QThread
Forum Updated to NodeBB v4.3 + New Features

How to correctly move event handler on QThread

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 747 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Q139
    wrote on last edited by Q139
    #1

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

    }

    ´´´

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You may want to take a look at the threaded fortune server example.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Q139
        wrote on last edited by Q139
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved