Skip to content Skip to sidebar Skip to footer

How To Emit Custom Events To The Event Loop In Pyqt

I am trying to emit custom events in PyQt. One widget would emit and another would listen to events, but the two widgets would not need to be related. In JavaScript, I would achiev

Solution 1:

In PyQt the following instruction:

document.addEventListener('Hello', () =>console.log('Got it'))

is equivalent

document.hello_signal.connect(lambda: print('Got it'))

In a similar way:

document.dispatchEvent(newEvent("Hello"))

is equivalent

document.hello_signal.emit()

But the big difference is the scope of the "document" object, since the connection is between a global element. But in PyQt that element does not exist.

One way to emulate the behavior that you point out is by creating a global object:

globalobject.py

from PyQt5 import QtCore
import functools

@functools.lru_cache()classGlobalObject(QtCore.QObject):
    def__init__(self):
        super().__init__()
        self._events = {}

    defaddEventListener(self, name, func):
        if name notin self._events:
            self._events[name] = [func]
        else:
            self._events[name].append(func)

    defdispatchEvent(self, name):
        functions = self._events.get(name, [])
        for func in functions:
            QtCore.QTimer.singleShot(0, func)

main.py

from PyQt5 import QtCore, QtWidgets
from globalobject import GlobalObject


classMainWindow(QtWidgets.QMainWindow):
    def__init__(self, parent=None):
        super().__init__(parent)
        button = QtWidgets.QPushButton(text="Press me", clicked=self.on_clicked)
        self.setCentralWidget(button)

    @QtCore.pyqtSlot()defon_clicked(self):
        GlobalObject().dispatchEvent("hello")


classWidget(QtWidgets.QWidget):
    def__init__(self, parent=None):
        super().__init__(parent)
        GlobalObject().addEventListener("hello", self.foo)
        self._label = QtWidgets.QLabel()
        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self._label)

    @QtCore.pyqtSlot()deffoo(self):
        self._label.setText("foo")


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w1 = MainWindow()
    w2 = Widget()
    w1.show()
    w2.show()
    sys.exit(app.exec_())

Post a Comment for "How To Emit Custom Events To The Event Loop In Pyqt"