How to pass signals between parallel widgets?
-
wrote on 26 Aug 2023, 05:27 last edited by delipizen
For example
from PyQt5.QtCore import pyqtSignal, QObject class A(QObject): def __init__(self): super().__init__() self.aa = AA() self.b = B() class AA(QObject): def __init__(self): super().__init__() self.aaa = AAA() class AAA(QObject): def __init__(self): super().__init__() self.aaaa = AAAA() class AAAA(QObject): sig = pyqtSignal() def __init__(self): super().__init__() class B(QObject): def __init__(self): super().__init__() def slot(self): print(123)
If i want to send signal from class AAAA to B, i must do this through class A. Is any method to easy to do this without class A?
-
For example
from PyQt5.QtCore import pyqtSignal, QObject class A(QObject): def __init__(self): super().__init__() self.aa = AA() self.b = B() class AA(QObject): def __init__(self): super().__init__() self.aaa = AAA() class AAA(QObject): def __init__(self): super().__init__() self.aaaa = AAAA() class AAAA(QObject): sig = pyqtSignal() def __init__(self): super().__init__() class B(QObject): def __init__(self): super().__init__() def slot(self): print(123)
If i want to send signal from class AAAA to B, i must do this through class A. Is any method to easy to do this without class A?
wrote on 26 Aug 2023, 08:53 last edited by JonB@delipizen
Not sure I understand, but will try.Two things. First, you don't/don't think of it as "send" signals from something to something. Rather, something sends a signal without any idea of anything in the outside world, so it never sends "to" anything; and things in the outside world subscribe and listen to the signal being emitted. Second, signals are not connected/sent between classes, they are connected/sent through instances of classes.
So you can only connect a signal from an instance of class AAAA to an instance of class B. And the only instance of class B you create is the
self.b = B()
. So that has to be visible when you do theconnect()
. Given your code you would need something like:a = A() a.aa.aaa.aaaa.sig.connect(a.b.slot)
If you don't want to reference the
a
instance, how else will you access (a) thea.aa.aaa.aaaa
instance to connect itssig
signal and (b) thea.b
instance to connect itsslot
slot? If you can arrange for that (e.g. perhaps passing them around as parameters) then you could connect them that way.Also, if it helps, you can define separate signals in multiple classes and "chain" connections to connect signals to slots, including chaining signals to signals. So for example, if you defined suitable extra signals, you could have something like:
a.aa.sigAA.connect(a.aa.aaa.sigAAA.connect(a.aa.aaa.aaaa.sig.connect(a.b.slot)))
[EDIT: Looking at above, I don't quite mean this one line of code! I mean connect each one's signal to emit a signal to fire the next one directly.]
You don't have to do that in one statement as shown here: you could connect instances at each level as you go, e.g.
class AAA.__init__()
could do the connection level to theself.aaaa = AAAA()
it creates, so the top-levela
which can access thea.b.slot
method does not need to know all the way down toa.aa.aaa.aaaa.sig.connect
, it can just do somea.aa.sigAA.connect()
.Don't know whether the above helps. Depends what you are trying to do.
1/2