我开发了一个不断“观察”特定文件夹的程序,当某个图像被复制到该文件夹中时,从另一个类调用一个函数并开始处理。这些图像很大的问题,所以每当图像处理功能开始时,ui就会冻结直到过程结束。我了解到QThread可能是解决方案,但是当我在课堂上有很多功能时,我无法弄明白如何应用...
更具体地说,请参阅步骤的顺序。
class Main(QtGui.QMainWindow):
def __init__():
functions = Functions()
...
self.something.signal.connect(self.functions.doStuff)
class Functions(QtCore.QObject):
def __init__():
imProc = ImageProcessing()
def doStuff():
initialImage = loadimage(...)
processedImage = imProc.process1(initialImage)
class ImageProcessing(QtCore.QObject)
def __init__():
def process1(original_image):
do maximum likelihood on the image
return segmented image
def process2(original_image):
do another image processing task
return segmented image
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
main.exec_()
问题是当process1()正在运行时,主窗口会冻结...请问你如何在后台分别从ui运行process1()?
提前致谢!
答案 0 :(得分:0)
首先是我的ImageProcessing类......
class ImageProcess(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def __del__(self):
self.exiting = True
self.wait()
def run(self):
#machine learning algorithm, which results a processed image...
self.emit( QtCore.SIGNAL('image'), processedImage)
return
我有我的功能课......
class Functions(QtCore.QObject):
def __init__():
self.improc = ImageProcess()
self.connect(self.improc, SIGNAL("image"), self.processImage)
self.segmented_image = []
self.improc.setPath(self.image)
self.improc.start()
def processImage(self,seg_img):
self.segmented_image = seg_img
然后像往常一样设置QMainWindow ......
希望对于那些正在使用Qthread,信号槽应用程序的人来说,它会有所帮助......