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 separate GUI and application code?
Forum Updated to NodeBB v4.3 + New Features

How to separate GUI and application code?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 764 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
    dpfrey
    wrote on 30 Jan 2023, 20:43 last edited by
    #1

    Hi,

    I'm trying to use PyQt5 as part of a larger application. I would describe this as an application that has an optional GUI attached. The GUI itself is not the focus of the project. As a result, I don't want the entire codebase littered with PyQt imports. The GUI and the main application run in two different threads. That's the background information. Here's my specific question:

    My application has the notion of a battery_percentage that is updated periodically in the application code. I want the GUI to display the battery percentage in a QLabel. Ultimately, in the GUI, I think this resolves to my_label.setText(f"{new_battery_percentage}%"), but there are two possibly related questions that I'm not clear on.

    1. How can I register a handler in the UI code to perform the setting of the text as mentioned above? I was reading a bit about signals/slots, but that seems like a way for one widget to communicate with another widget. What I am trying to do is have a non-widget publish something that is consumed by a widget.
    2. The application code runs in a different thread than the GUI, so how can I publish the battery updated "event" in a way that is safe and doesn't block the application thread?
    J 1 Reply Last reply 30 Jan 2023, 21:12
    0
    • D dpfrey
      30 Jan 2023, 20:43

      Hi,

      I'm trying to use PyQt5 as part of a larger application. I would describe this as an application that has an optional GUI attached. The GUI itself is not the focus of the project. As a result, I don't want the entire codebase littered with PyQt imports. The GUI and the main application run in two different threads. That's the background information. Here's my specific question:

      My application has the notion of a battery_percentage that is updated periodically in the application code. I want the GUI to display the battery percentage in a QLabel. Ultimately, in the GUI, I think this resolves to my_label.setText(f"{new_battery_percentage}%"), but there are two possibly related questions that I'm not clear on.

      1. How can I register a handler in the UI code to perform the setting of the text as mentioned above? I was reading a bit about signals/slots, but that seems like a way for one widget to communicate with another widget. What I am trying to do is have a non-widget publish something that is consumed by a widget.
      2. The application code runs in a different thread than the GUI, so how can I publish the battery updated "event" in a way that is safe and doesn't block the application thread?
      J Offline
      J Offline
      JonB
      wrote on 30 Jan 2023, 21:12 last edited by
      #2

      @dpfrey
      The answer to both is you can and should use signals/slots. They also work across your threads correctly. Signals/slots are for Qt objects, not just widgets. Write a single Python module which includes PyQt and emits signals, then rest of codebase can call these as necessary without them having to include/know about PyQt/Qt.

      D 1 Reply Last reply 31 Jan 2023, 18:08
      2
      • J JonB
        30 Jan 2023, 21:12

        @dpfrey
        The answer to both is you can and should use signals/slots. They also work across your threads correctly. Signals/slots are for Qt objects, not just widgets. Write a single Python module which includes PyQt and emits signals, then rest of codebase can call these as necessary without them having to include/know about PyQt/Qt.

        D Offline
        D Offline
        dpfrey
        wrote on 31 Jan 2023, 18:08 last edited by
        #3

        @JonB Thanks for your response. I think I have a pretty good idea what I have to do now.

        1 Reply Last reply
        0

        1/3

        30 Jan 2023, 20:43

        • 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