Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Use Signal in PySide or PyQt
Forum Updated to NodeBB v4.3 + New Features

Use Signal in PySide or PyQt

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 3 Posters 666 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.
  • J Offline
    J Offline
    jiajintao
    wrote on last edited by
    #1
    class MyLabel(QLabel):
        updateSignal = Signal(str)
    
        def __init__(self):
            super().__init__()
            self.updateSignal.connect(self.updateText)
    
        def updateText(self, text: str):
            self.setText(text)
    
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
    
            self.label = MyLabel()
            self.label.setParent(self)
            # use Signal
            self.label.updateSignal.emit('go')
            # use MyLabel obj function
            self.label.updateText('go')
    

    These two are the same in use in my opinion,
    And the second one could be a little faster,
    Both in development and in performance.

    • self.label.updateSignal.emit('go')
    • self.label.updateText('go')

    Or maybe I'm using the "Signa" l incorrectly.

    If there are more nesting of "Widgets", then I will use an "object" to store the signaled object

    class WidgetObject:
        Window1: Window
    
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
            ...
            WidgetObject.Window1 = self
    
    
    class Window2(QWidget):
        def __init__(self):
            super().__init__()
    
        def toWindow1LabelText(self):
            # use function
            WidgetObject.Window1.label.updateText('go2')
            # use signal
           WidgetObject.Window1.label.updateSignal.emit('go2')
    

    So how should I use "Signal" better?
    For more complex systems, how do I do the architecture, or as in the example above, use an "Object" to store the key "Widgets", I think this is too verbose

    Thank you very much. I'm confused.

    jsulmJ 1 Reply Last reply
    0
    • J jiajintao
      class MyLabel(QLabel):
          updateSignal = Signal(str)
      
          def __init__(self):
              super().__init__()
              self.updateSignal.connect(self.updateText)
      
          def updateText(self, text: str):
              self.setText(text)
      
      
      class Window(QWidget):
          def __init__(self):
              super().__init__()
      
              self.label = MyLabel()
              self.label.setParent(self)
              # use Signal
              self.label.updateSignal.emit('go')
              # use MyLabel obj function
              self.label.updateText('go')
      

      These two are the same in use in my opinion,
      And the second one could be a little faster,
      Both in development and in performance.

      • self.label.updateSignal.emit('go')
      • self.label.updateText('go')

      Or maybe I'm using the "Signa" l incorrectly.

      If there are more nesting of "Widgets", then I will use an "object" to store the signaled object

      class WidgetObject:
          Window1: Window
      
      
      class Window(QWidget):
          def __init__(self):
              super().__init__()
              ...
              WidgetObject.Window1 = self
      
      
      class Window2(QWidget):
          def __init__(self):
              super().__init__()
      
          def toWindow1LabelText(self):
              # use function
              WidgetObject.Window1.label.updateText('go2')
              # use signal
             WidgetObject.Window1.label.updateSignal.emit('go2')
      

      So how should I use "Signal" better?
      For more complex systems, how do I do the architecture, or as in the example above, use an "Object" to store the key "Widgets", I think this is too verbose

      Thank you very much. I'm confused.

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

      @jiajintao said in Use Signal in PySide or PyQt:

      self.label.updateSignal.emit('go')

      This makes no sence. Only the class where the signal is defined should emit it.
      If Window needs to update something in MyLabel, then MyLabel should provide a method to do so:

      class MyLabel(QLabel):
          def __init__(self):
              super().__init__()
      
          def updateText(self, text: str):
              self.setText(text)
      
      class Window(QWidget):
          def __init__(self):
              super().__init__()
      
              self.label = MyLabel()
              self.label.setParent(self)
              self.label.updateText('go')
      

      But this is also not needed in this case at all because MyLabel is already a QLabel and has setText method which can be called inside Window. So, I don't know what problem you're trying to solve...

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

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jiajintao
        wrote on last edited by
        #3

        @jsulm Sorry, it may be that my example is not good,maybe english is not good

        To put it simply,
        I don't know when I should customize the signal. When I use it in practice, I can solve the problem by calling this function directly

        JonBJ 1 Reply Last reply
        0
        • J jiajintao

          @jsulm Sorry, it may be that my example is not good,maybe english is not good

          To put it simply,
          I don't know when I should customize the signal. When I use it in practice, I can solve the problem by calling this function directly

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

          @jiajintao
          I don't know whether this will, help but....

          Your def updateText(self, text: str): is a public slot you have written. There are, effectively, always two ways a slot can be called:

          • connect a signal to call the slot, slot called when signal emitted; or
          • call the slot method directly, no signal/connection/emit.

          This is just the case with slots, they are normal methods which can be called directly as well as via signals. Which way you invoke them depends on context, e.g. when within the slot class you might call the slot directly when required without going via signals. OTOH, the "outside world" is more likely to raise a signal when something happens, and you will have done a connect to have the slot called. This helps keep the distinction/independence between the slot class and the rest of the program, so outside world does not need to know much about slot class implementation.

          1 Reply Last reply
          1
          • J jiajintao has marked this topic as solved on

          • Login

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