简体   繁体   English

在PyQt4中保持活动线程

[英]keep alive thread in PyQt4

I have a PyQt4 application, which at some point packs a big file using the tarfile module. 我有一个PyQt4应用程序,该应用程序有时使用tarfile模块打包一个大文件。 Since the tarfile module does not implement any callback strategy, it blocks and the Qt GUI gets unresponsive. 由于tarfile模块未实现任何回调策略,因此它将阻塞,并且Qt GUI无法响应。

I want the GUI to keep updating during that time. 我希望GUI在这段时间内保持更新。 The only possibility is a separate thread. 唯一的可能性是一个单独的线程。 So, I start a QThread. 因此,我启动了一个QThread。 What do I have to do in the QThread to make the GUI update itself? 我必须在QThread中做什么才能使GUI更新? As soon, as the tar process is finished, I want the thread to finish. 一旦tar进程完成,我希望线程完成。

Thanks! 谢谢! Nathan 内森

QThread's are pretty much identical to normal Python threads so you can just use normal communication methods. QThread与普通的Python线程几乎相同,因此您可以使用普通的通信方法。 However, QThreads also have a few signals available, so if you simply connect to those, than you're done. 但是,QThreads也有一些可用的信号,因此,如果您仅连接到这些信号,就完成了。

In your GUI code do something like this and you're pretty much done: 在您的GUI代码中执行以下操作,您已经完成了很多工作:

thread = Thread()
thread.finished.connect(gui.do_update_thingy)

There is also a terminated and started signal available which you can use :) 您还可以使用terminatedstarted信号:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM