栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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,并且措辞可能不太好),建议您检查上面发布的链接。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/662275.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号