tldr
When I emit a signal in script A
and catch it script B
to execute a slot there. Where should the mysignalA.connect(myslotB)
go?
In script A as: self.mysignalA.connect(B.myslotB)
or in script B as: A.mysignalA.connnect(self.myslotB)
Context
My Code is working properly and realiably, but: I have a big project with multiple Qthreads split up over several files and kind of lose track over all the signals/slots. Also multiple people work on it so that doesn’t help either.
In this specific example, I have a thread handling the UDP communication to send status updates to an external system, a thread handling the serial communication to the 3D printer and another thread handling some temperature modelling.
Now both the UDPsend
and the temperatureDtThread
require the temperature reading of the 3D printer at different rates/times, but I also can’t flood the serial communication with the same request twice so I decided on triggering a SLOT_request_temperature_reading
that does some serial communication and translation whenever someone somewhere needs the temperature and then emitting that temperature reading out again through the SIGNAL_temperature_reading
to be caught by whoever needs it at this point.
pseudo code
UDPComms.py
class UDPsend(QThread):
SIGNAL_temperature_req_UDP = pyqtSignal()
self.SIGNAL_temperature_req_UDP.connect(printer.SLOT_request_temperature_reading)
printer.SIGNAL_temperature_reading.connect(self.SLOT_get_temperature)
while True
#DoSomethingSomething
if required
self.SIGNAL_temperature_req_UDP.emit()
@pyqtslot(float)
SLOT_get_temperature(temperature_reading):
self.temperature=temperature_reading #So i can use the temperature in this class now
printer.py
class PrinterThread(QThred):
SIGNAL_temperature_reading = pyQtSignal(float)
if Temp_reading_requested = True
#DoSerialCommunicationAndStuff
SIGNAL_temperature_reading.emit(mytemperature)
@pyqtSlot()
SLOT_request_temperature_reading():
Temp_reading_requested = True
tempDT.py
class temperatureDtThread(QThread):
SIGNAL_temperature_req_DT = pyqtSignal()
self.SIGNAL_temperature_req_DT.connect(printer.SLOT_request_temperature_reading)
printer.SIGNAL_temperature_reading.connect(self.SLOT_temperature_read_out)
while True
#DoSomethingSomething
if required
SIGNAL_temperature_req_DT.emit
@pyqtSlot(float)
SLOT_temperature_read_out(temperature_reading):
self.temperature=temperature_reading #So i can use the temperature in this class now
Question
So right now in this case I try to do the connections from outside the printer.py
, but without much reasoning behind it. Is there a common good practice to where the connection of signals and slots should be defined?
Also: Please just roast my code if I am just misusing signals/slots completely