PyQt5 QObject:无法为处于不同线程中的父级创建子级

PyQt5 QObject:无法为处于不同线程中的父级创建子级,第1张

PyQt5 QObject:无法为处于不同线程中的父级创建子级

我将继续回答自己。受https://stackoverflow.com/a/33453124/1995261的启发,我通过实现以下方法解决了这一问题:

1)我创建了一个

worker.py
执行
_search_cast_
阻止菜单的方法的。当此方法完成搜索时,它会发出两个信号:a)一个通知他恢复了该信号
list
,以及b)该方法已完成。

#worker.pyfrom PyQt5.QtCore import QThread, QObject, pyqtSignal, pyqtSlotclass Worker(QObject):    finished = pyqtSignal()    intReady = pyqtSignal(list)    def __init__(self):        QObject.__init__(self)    @pyqtSlot()    def _search_cast_(self):        self.cc = casting()        self.cc.initialize_cast()        availablecc = self.cc.availablecc        self.intReady.emit(availablecc)        self.finished.emit()

2)在“

main.py
我转储了以下内容”并尝试在代码内用注释进行解释:

#main.pyfrom PyQt5.QtCore import QThread, QObject, pyqtSignal, pyqtSlotimport worker # This is to import worker.pyclass menubar(object):    def __init__(self):        signal.signal(signal.SIGINT, signal.SIG_DFL)        self.cc.cast = None        self.systray = True        self.stopped = False        self.obj = worker.Worker()  # The worker is started with no parent!        self.thread = QThread()  # We initialise the Qthread class with no parent!        self.obj.intReady.connect(self.onIntReady) # We receive the signal that the list is ready        self.obj.moveToThread(self.thread) # Moving the object to the thread        self.obj.finished.connect(self.thread.quit) # When the method is finished we receive the signal that it is finished        self.thread.started.connect(self.obj._search_cast_) # We need to connect the above with the desired method inside the work.py        self.app = QtWidgets.QApplication(sys.argv)        def search_menu(self): self.SearchAction = self.menu.addAction("Search") self.SearchAction.triggered.connect(self.search_cast)        def onIntReady(self, availablecc):     # This method receives the list from the worker print ('availablecc', availablecc)  # This is for debugging reasons to verify that I receive the list with the correct content self.availablecc = availablecc        def search_cast(self):   #This method starts the thread when  self.SearchAction is triggered args.select_cc = True self.thread.start()

这样,在搜索

list
菜单时不会被阻塞,屏幕上不会显示任何错误,并且
threads
监控它们的次数
activity monitor
保持正确。

希望对大家有帮助。有关更精确的信息(我仍在学习PyQt,并且措辞可能不太好),建议您检查上面发布的链接。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5664231.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存