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 use QTcpSocket with Threads
Forum Update on Monday, May 27th 2025

How to use QTcpSocket with Threads

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 745 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.
  • G Offline
    G Offline
    GCE_
    wrote on last edited by
    #1

    Hi guys!
    In my applicatin I have three classes (3 Measurement modules) which are communication over TCP.
    All have different IP and Ports. My problem is, that I have errors with data transmission and some sensor values are wrong because reading it needs longer. With waitForReadyRead it works but it blocks the whole communication and I have then problems with the other measurement modules
    I want to use the Sockets with Threads and I have no prior experience with threads in QT.
    I hope you guys can help me.

    Best regards

    JonBJ Gojir4G 2 Replies Last reply
    0
    • G GCE_

      Hi guys!
      In my applicatin I have three classes (3 Measurement modules) which are communication over TCP.
      All have different IP and Ports. My problem is, that I have errors with data transmission and some sensor values are wrong because reading it needs longer. With waitForReadyRead it works but it blocks the whole communication and I have then problems with the other measurement modules
      I want to use the Sockets with Threads and I have no prior experience with threads in QT.
      I hope you guys can help me.

      Best regards

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @GCE_ said in How to use QTcpSocket with Threads:

      I want to use the Sockets with Threads and I have no prior experience with threads in QT.

      Why? As you say, threads are indeed difficult, especially for a beginner!

      Qt sockets are asynchronous by nature, meaning they don't block and you can have multiple sockets opened/serviced without one blocking/interfering with another.

      As you say, if you use the waitFor...() calls you will block on that socket, and would indeed need threads to prevent that. Don't use those calls at all! Look at the signals/slots in QTcpSocket/QAbstractSocket and use only those, then you won't need any blocking calls or threads :)

      1 Reply Last reply
      4
      • G GCE_

        Hi guys!
        In my applicatin I have three classes (3 Measurement modules) which are communication over TCP.
        All have different IP and Ports. My problem is, that I have errors with data transmission and some sensor values are wrong because reading it needs longer. With waitForReadyRead it works but it blocks the whole communication and I have then problems with the other measurement modules
        I want to use the Sockets with Threads and I have no prior experience with threads in QT.
        I hope you guys can help me.

        Best regards

        Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by
        #3

        @GCE_ Hi, you don't need threads for that, instead of waitForReadyRead()you can use readyRead() signal and read from socket at this time. This will avoid blocking your event loop and freeze your GUI.
        see Fortune Client Example

        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 Extensions
        • Unsolved