我们有一个执行不同查询的应用程序.它最多启动四个线程,并在它们上运行提取.
那部分看起来像这样:
if len(self.threads) == 4:
self.__maxThreadsMsg(base)
return False
else:
self.threads.append(Extractor(self.ui, base))
self.threads[-1].start()
self.__extractionMsg(base)
return True
我们的Extractor类继承了QThread:
class Extractor(QThread):
def init(self, ui, base):
QThread.__init__(self)
self.ui = ui
self.base = base
def run(self):
self.run_base(base)
和self.ui设置为Ui_MainWindow():
class Cont(QMainWindow):
def __init__(self, parent=None):
QWidget.__init__(self,parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
在继续之前,有一个特定的基础将数据发送给用户(返回主窗口)(在这种情况下,带有两个按钮的弹出窗口):
#This code is in the main file inside a method, not in the Extractor class
msg_box = QMessagebox()
msg_box.setText('Quantity in base: '.format(n))
msg_box.setInformativeText('Would you like to continue?')
msg_box.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
signal = msg_box.exec_()
如何在特定点暂停线程,显示窗口(我相信将返回主线程)并返回工作线程,传递按钮点击事件?
我读了一些关于信号的内容,但它似乎令人困惑,因为这是我第一次处理线程.
编辑:读完这个问题后:Similar question,我把代码修改为:
在Cont类的内部方法
thread = QThread(self)
worker = Worker()
worker.moveToThread(thread)
worker.bv.connect(self.bv_test)
thread.started.connect(worker.process()) # This, unlike in the linked question..
#doesn't work if I remove the parentheses of the process function.
#If I remove it, nothing happens and I get QThread: "Destroyed while thread is still running"
thread.start()
@pyqtSlot(int)
def bv_test(self, n):
k = QMessageBox()
k.setText('Quantity: {}'.format(n))
k.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
ret = k.exec_()
return ret
这是Worker类:
class Worker(QObject):
#Signals
bv = pyqtSignal(int)
def process(self):
self.bv.emit(99)
现在我只需要弄清楚如何将ret值发送回工作线程,以便启动第二个进程.我也一直收到这个错误:
TypeError:connect()slot参数应该是可调用的或信号,而不是’NoneType’