Jump to content

User:Atalanttore/pyqtSignal

From Qt Wiki

Very easy example for defining and emiting a pyqtSignal

Requirements

  • PyQt5
  • Python 3

Code

from PyQt5.QtCore import QObject, pyqtSignal


class Receiver:

    def slot(self, local_variable):
        print(local_variable)


class Sender(QObject):  # must inherit from QObject.

    # Define a new signal called 'signal'.
    # Write the data type of the argument passed with the signal in parentheses. Pack several data types in a list.
    signal = pyqtSignal(str)

    def __init__(self):
        super().__init__()

    def emit_signal(self):
        self.signal.emit("Hello World")


def main():
    receiver = Receiver()
    sender = Sender()

    sender.signal.connect(receiver.slot)
    sender.emit_signal()


if __name__ == "__main__":
    main()