PySide pasando señales de QThread a una ranura en otro QThread
Resolví mi problema moviendo elmySubQThread
run()
en elmyQThread
run()
Dicho esto, todavía me gustaría saber por qué lo que estaba intentando antes no funcionó.
Soy bastante nuevo en el enhebrado. Me encuentro con este problema y creo que puedo estar abordando las cosas mal, de todos modos aquí va. Estoy abierto a un enfoque diferente. Sé que esto puede ser un poco complicado de seguir.
Tengo una GUI que hace un nuevo derivadoQThread
vamos a llamarlomyQThread
dentro de ese hilo, estoy ejecutando un proceso que crea otro hilo llamadomySubQThread
El problema que tengo es el siguiente, tengo señales definidas en mi GUI, por ejemplo:signalA = QtCore.Signal(int)
y una ranura enmyQThread
La ranura en elmySubQThread
Parece que nunca recibe la señal.
Aquí hay un ejemplo de trabajo. (modificado algo)
from PySide import QtCore, QtGui
import time
class myQThread(QtCore.QThread):
myThreadSignal = QtCore.Signal(int)
def __init__(self, parent):
super(myQThread, self).__init__(parent=parent)
def run(self):
self.subThread = mySubQThread(parent=self)
self.myThreadSignal.connect(self.subThread.sub_thread_slot)
self.myThreadSignal.connect(self.test_slot)
print "starting subthread..."
self.subThread.start()
while self.subThread.isRunning():
print "myQThread is alive!"
time.sleep(1)
print "myQThread exiting run..."
@QtCore.Slot(int)
def my_thread_slot(self, a):
print "1b) Made it here!"
self.myThreadSignal.emit(a)
@QtCore.Slot(int)
def test_slot(self, a):
print "2a) Made it here!"
class mySubQThread(QtCore.QThread):
mySubSignalA = QtCore.Signal(int)
def __init__(self, parent):
super(mySubQThread, self).__init__(parent=parent)
self._abort = False
def run(self):
#Do some processing
#Wait for signal
self._abort = False
while not self._abort:
print "mySubQThread is alive!"
time.sleep(1)
print "mySubQThread exiting run..."
@QtCore.Slot(int)
def sub_thread_slot(self, a):
print "2b)Never make it here!"
self._abort = True
class myWidget(QtGui.QWidget):
myWidgetSignal = QtCore.Signal(int)
def __init__(self, parent=None):
super(myWidget, self).__init__(parent=parent)
#simple Widget to test this out....
myLayout = QtGui.QVBoxLayout()
self.runButton = QtGui.QPushButton("run")
self.runButton.clicked.connect(self.run_button_pressed)
self.otherButton = QtGui.QPushButton("other")
self.otherButton.clicked.connect(self.other_button_pressed)
myLayout.addWidget(self.runButton)
myLayout.addWidget(self.otherButton)
self.setLayout(myLayout)
@QtCore.Slot()
def run_button_pressed(self):
self.processThread = myQThread(self)
self.myWidgetSignal.connect(self.processThread.my_thread_slot)
self.myWidgetSignal.connect(self.test_slot)
self.processThread.start()
@QtCore.Slot()
def other_button_pressed(self):
self.myWidgetSignal.emit(1)
@QtCore.Slot(int)
def test_slot(self, a):
print "1a) Made it here!"
if __name__ == "__main__":
import sys
myApp = QtGui.QApplication(sys.argv)
myWin = myWidget()
myWin.show()
sys.exit(myApp.exec_())
Aquí hay algunos resultados de muestra:
Tenga en cuenta que si cambia la línea:
self.subThread = mySubQThread(parent=self)
a
self.subThread = mySubQThread(parent=None)
deja de quejarse como lo hace en la salida de muestra. ninguno muestra que llegue a 2B
QObject: Cannot create children for a parent that is in a different thread.
(Parent is myQThread(0x3c3faf0), parent's thread is QThread(0x2792548), current thread is myQThread(0x3c3faf0)
starting subthread...
myQThread is alive!mySubQThread is alive!
mySubQThread is alive!
myQThread is alive!
1b) Made it here!
2a) Made it here!
1a) Made it here!