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. Interaction between threads and GUI
QtWS25 Last Chance

Interaction between threads and GUI

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 779 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.
  • D Offline
    D Offline
    ducdx
    wrote on last edited by
    #1

    Dear all,
    I'm a newbie in Qt (also in Python) and I'm facing an issue that needs help. I created a desktop application using PyQt5 include:

    • a socket connection to get data
    • a thread to receive data from the socket and parse data to struct (called thread X)
    • After parsing data done in thread X, I want to send data and notify to update the main window (GUI) but I don't know how to interact between Python thread and main thread (GUI)
      Could you please give me some advice or solutions? Thank you very much
    jsulmJ JonBJ 2 Replies Last reply
    0
    • D ducdx

      Dear all,
      I'm a newbie in Qt (also in Python) and I'm facing an issue that needs help. I created a desktop application using PyQt5 include:

      • a socket connection to get data
      • a thread to receive data from the socket and parse data to struct (called thread X)
      • After parsing data done in thread X, I want to send data and notify to update the main window (GUI) but I don't know how to interact between Python thread and main thread (GUI)
        Could you please give me some advice or solutions? Thank you very much
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @ducdx said in Interaction between threads and GUI:

      but I don't know how to interact between Python thread and main thread (GUI)

      Using signals/slots.
      In your receiver thread emit a signal which is connected to a slot in your GUI.

      But you should really rethink your design: is there really a need for threads at all? Qt networking (like most of Qt) is assynchronous, so usually there is no need to overcomplicate things with threads.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      1
      • D ducdx

        Dear all,
        I'm a newbie in Qt (also in Python) and I'm facing an issue that needs help. I created a desktop application using PyQt5 include:

        • a socket connection to get data
        • a thread to receive data from the socket and parse data to struct (called thread X)
        • After parsing data done in thread X, I want to send data and notify to update the main window (GUI) but I don't know how to interact between Python thread and main thread (GUI)
          Could you please give me some advice or solutions? Thank you very much
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @ducdx
        Hello and welcome.

        Cannot emphasize enough what @jsulm has said about " is there really a need for threads at all?". These days every beginner seems to come here and start out wanting to use threads. Threading is complicated, easy to get wrong, and mostly/often not needed.

        Please at least start out with no threads. Do any parsing in the one and only main thread (in a slot). After you have it working, only then consider threading your code. Unless your "parsing" is really reading a lot of data or doing some very heavy computations it probably simply will not require a separate thread.

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @ducdx said in Interaction between threads and GUI:

          but I don't know how to interact between Python thread and main thread (GUI)

          Using signals/slots.
          In your receiver thread emit a signal which is connected to a slot in your GUI.

          But you should really rethink your design: is there really a need for threads at all? Qt networking (like most of Qt) is assynchronous, so usually there is no need to overcomplicate things with threads.

          D Offline
          D Offline
          ducdx
          wrote on last edited by
          #4

          @jsulm Thank you for your answer very much
          I am developing an application that connects to a hardware device via a socket. I used thread to receive data. As your idea, do I not need a thread to receive data from the socket? Can I call the recv() function on the main thread, right?

          jsulmJ JonBJ 2 Replies Last reply
          0
          • D ducdx

            @jsulm Thank you for your answer very much
            I am developing an application that connects to a hardware device via a socket. I used thread to receive data. As your idea, do I not need a thread to receive data from the socket? Can I call the recv() function on the main thread, right?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @ducdx said in Interaction between threads and GUI:

            Can I call the recv() function on the main thread, right?

            So, you are not using Qt for networking (https://doc.qt.io/qt-5.15/qtnetwork-index.html)?
            If not - why?
            If you're using blocking calls then of course you need a second thread. But why not simply use Qt for networking?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply
            1
            • D ducdx

              @jsulm Thank you for your answer very much
              I am developing an application that connects to a hardware device via a socket. I used thread to receive data. As your idea, do I not need a thread to receive data from the socket? Can I call the recv() function on the main thread, right?

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

              @ducdx
              Not quite if you choose to use recv(), because that is a blocking call, and if data is not available it will block interaction with your UI thread if called from there.

              Normally we do not use such functions from Qt any longer. Consider writing your code to use QTcpSocket instead of low-level socket functions like recv(). Then you can use signals and slots to receive and process the data from the main UI thread without needing a separate thread.

              D 1 Reply Last reply
              3
              • jsulmJ jsulm

                @ducdx said in Interaction between threads and GUI:

                Can I call the recv() function on the main thread, right?

                So, you are not using Qt for networking (https://doc.qt.io/qt-5.15/qtnetwork-index.html)?
                If not - why?
                If you're using blocking calls then of course you need a second thread. But why not simply use Qt for networking?

                D Offline
                D Offline
                ducdx
                wrote on last edited by
                #7

                @jsulm Ah, because I learn Python programming first, and then I learn Qt to build a GUI desktop application. Therefore, I use Python socket. I will try QTcpSocket instead

                1 Reply Last reply
                1
                • JonBJ JonB

                  @ducdx
                  Not quite if you choose to use recv(), because that is a blocking call, and if data is not available it will block interaction with your UI thread if called from there.

                  Normally we do not use such functions from Qt any longer. Consider writing your code to use QTcpSocket instead of low-level socket functions like recv(). Then you can use signals and slots to receive and process the data from the main UI thread without needing a separate thread.

                  D Offline
                  D Offline
                  ducdx
                  wrote on last edited by
                  #8

                  @JonB Yeap, thank you very much
                  I will try it

                  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