Migración del modelo heredado QThread a Worker
Entonces, a través de mucha ayuda en mis preguntas anteriores (Interrumpir el sueño QThread yPySide pasando señales de QThread a una ranura en otro QThread) Decidí intentar cambiar de lo heredadoQThread
modelo al modelo de trabajador. Estoy pensando que debería quedarme con elQThread
modelo ya que tenía ese trabajo, y el otro modelo no lo es. Sin embargo, no estoy seguro de por qué el modelo Worker no funciona para mí.
Estoy intentando hacer esto, por favor avíseme si hay algo inherentemente incorrecto en mi metodología.
tengo unQtGui.QWidget
Esa es mi GUI principal. Estoy usando unQPushButton
para indicar que he intentado reducir el código a lo básico de dónde creo que está el problema. He verificado quedatagramHandled
Signal
se emite pero elpacket_handled
Slot
no parece ser llamado
class myObject(QtCore.QObject):
def __init__(self):
super(myObject, self).__init__()
self.ready=False
@QtCore.Slot()
def do_work(self):
#send a packet
self.ready=False
while not self.ready:
time.sleep(0.01)
@QtCore.Slot(int)
def packet_handled(self, errorCode):
print "Packet received."
self.ready = True
class myWidget(QtGui.QWidget):
datagramHandled = QtCore.Signal(int)
startRunThread = QtCore.Signal()
def __init__(self,parent=None, **kwargs):
super(myWidget, self).__init__(parent=parent)
# Bunch of GUI setup stuff (working)
self.myRunThread = QtCore.QThread()
@QtCore.Slot()
def run_command(self):
self.myRunObj = myObject()
self.myRunObj.moveToThread(self.myRunThread)
self.datagramHandled.connect(self.myRunObj.packet_handled)
self.startRunThread.connect(self.myRunObj.do_work)
self.myRunThread.start()
self.startRunThread.emit()
@QtCore.Slot()
def handle_datagram(self):
#handle the incoming datagram
errorCode = 0
self.datagramHandled.emit(errorCode)