简体   繁体   中英

PyQt signals only picked up by slots of

I have one EventDispatcher class that communicates with four other objects through pyqt's signal-slot method. On http://pyqt.sourceforge.net/Docs/PyQt5/signals_slots.html I find that you can connect a single signal to multiple slots and that a slot can be any Python callable (without further requirements).

Now I am observing some weird behavior. I have two objects that are subclasses of QtWidget and two objects that are simple Python objects. Each of these objects has a function that serves as a slot to the EventDispatcher's signal. Whenever the signal is emitted, the two QWidget child objects do seem to receive it, but the two normal python objects don't. Why do the slot functions of those normal Python objects not get called, but those of the widgets do? Does a function/class/object have to meet specific demands to function as a slot that I cannot find in the documentation? I have already tried to make the two Python base object subclasses of QtCore.QObject or QtWidgets.QWidget, but to no avail.

I would post my code below, but the project is already quite complex and the codebase quite long. I am hoping my problem is apparent from my description above, and that it is expected Qt behavior and/or a simple thing that I am overlooking..

Got it. I created my listener variables locally inside a function call where I also attached them to the signal. After the function returned these locally scoped variables were of course discarded. My assumption was that the references to my listener objects would be kept alive because their functions were connected as a slot, but this is not the case. By making the variables referencing the listener objects variables of the object instance in which I created them, this problem was solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM