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. Block GUI when waiting for QTcpSocket data
QtWS25 Last Chance

Block GUI when waiting for QTcpSocket data

Scheduled Pinned Locked Moved General and Desktop
qtcpsocketnetworking
3 Posts 3 Posters 1.5k 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.
  • F Offline
    F Offline
    FredT
    wrote on 11 Aug 2016, 15:58 last edited by FredT 8 Nov 2016, 16:02
    #1

    Hello everyone,

    I am writing a program to interact with a network-based API using Qt. The interaction with the API is made with XML messages (both queries and results)

    I implemented the communication and data processing in a class in a shared library project and I have a QMainWindow in which the user can enter the connection details. When clicking on the connect button, the following should happen: 1. An instance of the connecting class is created. 2. A connection message is sent to the API to get the session ID. The answer is parsed and a session ID is stored in the class instance. 3. A message is sent to the API the get some field information. The XML is then parsed to extract the required field information to get data from the API. 4. Another message is sent to get the data matching the fields. The XML answer is then parsed and stored in a data structure for processing. 5. The data is then processed and finally displayed to the user.

    I made a simple console program to test the library and it is working fine - no message is sent before all the data from the previous message has been processed. However, when I implement the same process in a QMainWindow instance, no wait occurs and messages are sent one after another without waiting.

    How can I block the GUI thread to wait for full processing before sending the next message?

    Thanks

    PS: The program I am working on is an internal tool at my company and the users will be mostly me (and maybe, maybe, one or two other persons).
    PPS: It is a re-post from my stack overflow question: http://stackoverflow.com/questions/38900091/block-gui-when-waiting-for-qtcpsocket-data

    K 1 Reply Last reply 11 Aug 2016, 16:03
    0
    • F FredT
      11 Aug 2016, 15:58

      Hello everyone,

      I am writing a program to interact with a network-based API using Qt. The interaction with the API is made with XML messages (both queries and results)

      I implemented the communication and data processing in a class in a shared library project and I have a QMainWindow in which the user can enter the connection details. When clicking on the connect button, the following should happen: 1. An instance of the connecting class is created. 2. A connection message is sent to the API to get the session ID. The answer is parsed and a session ID is stored in the class instance. 3. A message is sent to the API the get some field information. The XML is then parsed to extract the required field information to get data from the API. 4. Another message is sent to get the data matching the fields. The XML answer is then parsed and stored in a data structure for processing. 5. The data is then processed and finally displayed to the user.

      I made a simple console program to test the library and it is working fine - no message is sent before all the data from the previous message has been processed. However, when I implement the same process in a QMainWindow instance, no wait occurs and messages are sent one after another without waiting.

      How can I block the GUI thread to wait for full processing before sending the next message?

      Thanks

      PS: The program I am working on is an internal tool at my company and the users will be mostly me (and maybe, maybe, one or two other persons).
      PPS: It is a re-post from my stack overflow question: http://stackoverflow.com/questions/38900091/block-gui-when-waiting-for-qtcpsocket-data

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 11 Aug 2016, 16:03 last edited by
      #2

      @FredT
      Hello,

      You can start a local event loop, but this is discouraged. If you still insist on doing it:

      QEventLoop loop;
      QObject::connect(sender, &SenderClass::signal, &loop, &QEventLoop::quit);
      loop.exec();
      

      Where sender is a QObject that will signal the event loop to quit, and the SenderClass::signal() is the signal that should be emitted and will stop the event loop's waiting.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • V Offline
        V Offline
        VRonin
        wrote on 11 Aug 2016, 16:08 last edited by
        #3

        I agree with stack overflow. Your design is flawed. The state machine behind the communication should not depend in any way on the interface

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1

        1/3

        11 Aug 2016, 15:58

        • Login

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