PySide passando sinais do QThread para um slot em outro QThread
Resolvi meu problema movendo omySubQThread
run()
nomyQThread
run()
Dito isto, eu ainda gostaria de saber por que o que estava tentando antes não funcionou.
Eu sou muito novo no segmento. Estou enfrentando esse problema e acho que posso estar abordando coisas erradas, de qualquer forma, aqui vai. Estou aberto a uma abordagem diferente, sei que isso pode ser um pouco complicado de seguir.
Eu tenho uma GUI que cria uma nova derivadaQThread
vamos chamá-lomyQThread
dentro desse thread, estou executando um processo que cria outro thread, chame-omySubQThread
o problema que estou tendo é o seguinte, tenho sinais definidos na minha GUI, por exemplo:signalA = QtCore.Signal(int)
e um slotmyQThread
O slot nomySubQThread
parece nunca receber o sinal.
Aqui está um exemplo de trabalho. (modificado um pouco)
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_())
Aqui está um exemplo de saída:
Observe que se você alterar a linha:
self.subThread = mySubQThread(parent=self)
para
self.subThread = mySubQThread(parent=None)
ele para de reclamar, como ocorre na saída da amostra. nem mostra que chega 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!