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

Python-PyQt中带有QThread的后台线程

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

Python-PyQt中带有QThread的后台线程

我创建了一个小示例,展示了3种不同的简单线程处理方式。我希望它能帮助你找到解决问题的正确方法。

import sysimport timefrom PyQt5.QtCore import (QCoreApplication, QObject, QRunnable, QThread,    QThreadPool, pyqtSignal)# Subclassing QThread# http://qt-project.org/doc/latest/qthread.htmlclass AThread(QThread):    def run(self):        count = 0        while count < 5: time.sleep(1) print("A Increasing") count += 1# Subclassing QObject and using moveToThread# http://blog.qt.digia.com/blog/2007/07/05/qthreads-no-longer-abstractclass SomeObject(QObject):    finished = pyqtSignal()    def long_running(self):        count = 0        while count < 5: time.sleep(1) print("B Increasing") count += 1        self.finished.emit()# Using a QRunnable# http://qt-project.org/doc/latest/qthreadpool.html# Note that a QRunnable isn't a subclass of QObject and therefore does# not provide signals and slots.class Runnable(QRunnable):    def run(self):        count = 0        app = QCoreApplication.instance()        while count < 5: print("C Increasing") time.sleep(1) count += 1        app.quit()def using_q_thread():    app = QCoreApplication([])    thread = AThread()    thread.finished.connect(app.exit)    thread.start()    sys.exit(app.exec_())def using_move_to_thread():    app = QCoreApplication([])    objThread = QThread()    obj = SomeObject()    obj.moveToThread(objThread)    obj.finished.connect(objThread.quit)    objThread.started.connect(obj.long_running)    objThread.finished.connect(app.exit)    objThread.start()    sys.exit(app.exec_())def using_q_runnable():    app = QCoreApplication([])    runnable = Runnable()    QThreadPool.globalInstance().start(runnable)    sys.exit(app.exec_())if __name__ == "__main__":    #using_q_thread()    #using_move_to_thread()    using_q_runnable()


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

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

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