PyQt4、QThread 和在不冻结 GUI 的情况下打开大文件 [英] PyQt4, QThread and opening big files without freezing the GUI

查看:63
本文介绍了PyQt4、QThread 和在不冻结 GUI 的情况下打开大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下如何从磁盘读取大文件并保持 PyQt4 UI 响应(不被阻止).我已将文件的负载移至 QThread 子类,但我的 GUI 线程被冻结.有什么建议么?我认为它一定与 GIL 有关,但我不知道如何对其进行排序?

I would like to ask how to read a big file from disk and maintain the PyQt4 UI responsive (not blocked). I had moved the load of the file to a QThread subclass but my GUI thread get freezed. Any suggestions? I think it must be something with the GIL but I don't know how to sort it?

我正在使用 GDCM 项目中的 vtkGDCMImageReader 读取多帧 DICOM 图像并使用 vtk 和 pyqt4 显示它.我在不同的线程 (QThread) 中执行此加载,但我的应用程序会冻结,直到加载图像.这是一个示例代码:

I am using vtkGDCMImageReader from the GDCM project to read a multiframe DICOM image and display it with vtk and pyqt4. I do this load in a different thread (QThread) but my app freeze until the image is loaded. here is an example code:

class ReadThread(QThread): 
    def __init__(self, file_name): 
        super(ReadThread, self).__init__(self) 
        self.file_name = file_name 
        self.reader.vtkgdcm.vtkGDCMImageReader()

    def run(self): 
        self.reader.SetFileName(self.file_name) 
        self.reader.Update() 
        self.emit(QtCore.SIGNAL('image_loaded'), self.reader.GetOutput())

推荐答案

我猜你是直接调用 run 来开始线程.这会使 GUI 冻结,因为您没有激活线程.

I'm guessing you're directly calling the run to begin the thread. That would make the GUI freeze because you're not activating the thread.

所以你会错过那里的 start,它会间接和正确地调用 run:

So you'd be missing the start there, which would indirectly and properly call the run:

thread = ReadThread()
thread.begin()

class ReadThread(QThread): 
    def __init__(self, file_name): 
        super(ReadThread, self).__init__(self) 
        self.file_name = file_name 
        self.reader.vtkgdcm.vtkGDCMImageReader()

    def run(self): 
        self.reader.SetFileName(self.file_name) 
        self.reader.Update() 
        self.emit(QtCore.SIGNAL('image_loaded'), self.reader.GetOutput())

    def begin(self): 
        self.start()

这篇关于PyQt4、QThread 和在不冻结 GUI 的情况下打开大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆