Custom signals and slots using pya

edited August 1 in Python scripting

Hello,

I am trying to create new class derived from QObject and emit signals from it. However, I can't find any documentation or examples about how to do that properly. Can someone please help with that?

Something like

import pya
class MyClass(pya.QObject):
  def __init__(self, parent = None):
    super().__init__(parent)      
    self.signal = ...Something...

  def doAction(self)
    self.signal.emit("Signal argument")

def receive(v):
    print(f"Received: {v}")

def main():
   c = MyClass()
   c.signal(receive)
   c.doAction()

if __name__=="__main__":
   main()

Hopefully to get "Signal argument" in console log.

thanks,
Alex

Comments

  • edited August 3

    Hi Alex,

    signals and slots are concepts Qt introduced for supporting dynamic code paths in C++, which is a (extremely) static language.

    You do not need signals and slots and dynamic languages like Python. Just use instance method objects, lambdas or simply duck typing to achieve the same effect.

    Signals and slots are supported in KLayout, because they are needed for interfacing with the native Qt objects, that's all. There is no support for defining new signals. Slots for native signals are implemented by simply attaching an instance method to a native signal. So there is also no need for defining custom slots.

    Matthias

  • Thank you!

Sign In or Register to comment.