Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I use a QTcpSocket from a subclass of QThread?
Qt 6.12 LTS is out! See what's new in the release blog

How do I use a QTcpSocket from a subclass of QThread?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 609 Views
  • 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.
  • A Offline
    A Offline
    andyP
    wrote on last edited by
    #1

    I am reading from a Tcp socket in a separate thread:

    class ReadFromTcpThread : public QThread {
    	Q_OBJECT
    public:
    	ReadFromTcpThread(const std::string& tcpAddress, unsigned int port) : m_tcpAddress(tcpAddress), m_port(port) { };
    	void run() override;
    private slots:
    	void connected();
    	void errorOccurred(QAbstractSocket::SocketError e_socketError);
    	void processPendingBytes();
    private:
    	std::string m_tcpAddress;
    	unsigned int m_port;
    	QTcpSocket* m_tcpSocket = nullptr;
    	QByteArray m_data;
    };
    
    void ReadFromTcpThread::run() {
    	m_tcpSocket = new QTcpSocket;
    	m_tcpSocket->moveToThread(QThread::currentThread());
    	m_tcpSocket->connectToHost(m_tcpAddress.c_str(), m_port);
    	connect(m_tcpSocket, &QAbstractSocket::connected, this, &ReadFromTcpThread::connected);
    	connect(m_tcpSocket, &QAbstractSocket::errorOccurred, this, &ReadFromTcpThread::errorOccurred);
    	connect(m_tcpSocket, &QTcpSocket::readyRead, this, &ReadFromTcpThread::processPendingBytes);
    	exec(); // run event loop
    }
    

    Is this correct code?
    It seems to work. If I used the QThread as the parent of the socket I got an error. Since the socket is created inside the run method I do not need to moveToThread()?

    Christian EhrlicherC 1 Reply Last reply
    0
    • A andyP

      I am reading from a Tcp socket in a separate thread:

      class ReadFromTcpThread : public QThread {
      	Q_OBJECT
      public:
      	ReadFromTcpThread(const std::string& tcpAddress, unsigned int port) : m_tcpAddress(tcpAddress), m_port(port) { };
      	void run() override;
      private slots:
      	void connected();
      	void errorOccurred(QAbstractSocket::SocketError e_socketError);
      	void processPendingBytes();
      private:
      	std::string m_tcpAddress;
      	unsigned int m_port;
      	QTcpSocket* m_tcpSocket = nullptr;
      	QByteArray m_data;
      };
      
      void ReadFromTcpThread::run() {
      	m_tcpSocket = new QTcpSocket;
      	m_tcpSocket->moveToThread(QThread::currentThread());
      	m_tcpSocket->connectToHost(m_tcpAddress.c_str(), m_port);
      	connect(m_tcpSocket, &QAbstractSocket::connected, this, &ReadFromTcpThread::connected);
      	connect(m_tcpSocket, &QAbstractSocket::errorOccurred, this, &ReadFromTcpThread::errorOccurred);
      	connect(m_tcpSocket, &QTcpSocket::readyRead, this, &ReadFromTcpThread::processPendingBytes);
      	exec(); // run event loop
      }
      

      Is this correct code?
      It seems to work. If I used the QThread as the parent of the socket I got an error. Since the socket is created inside the run method I do not need to moveToThread()?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @andyP said in How do I use a QTcpSocket from a subclass of QThread?:

      Is this correct code?

      No since all your slot are executed in the main thread because your ReadFromTcpThread lives in the main thread.

      1. don't use threads for QTcpSocket - it's not needed
      2. if you use a thread, use the worker approach to make sure all your stuff is done in your created thread.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4

      • Login

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