2011-01-06 73 views
2

这是PyQT遇到的最大麻烦:我为我的应用程序(我必须缩略图大量缩略图)缩小了一个缩略图线,它看起来好像可以工作(并且它几乎可以使用确实)。我的主要问题是此错误消息,每当我从我的线程发送SIGNALPyQt:我如何处理QThread中的QPixmaps?

QPixmap: It is not safe to use pixmaps outside the GUI thread 

我无法弄清楚如何来解决这个问题。我试过通过我的SIGNAL传递QIcon,但仍然会产生相同的错误。如果有帮助,下面是它对付这东西的代码块:

Thumbnailer类:

class Thumbnailer(QtCore.QThread): 
    def __init__(self, ListWidget, parent = None): 
    super(Thumbnailer, self).__init__(parent) 
    self.stopped = False 
    self.completed = False 
    self.widget = ListWidget 

    def initialize(self, queue): 
    self.stopped = False 
    self.completed = False 
    self.queue = queue 

    def stop(self): 
    self.stopped = True 

    def run(self): 
    self.process() 
    self.stop() 

    def process(self): 
    for i in range(self.widget.count()): 
     item = self.widget.item(i) 

     icon = QtGui.QIcon(str(item.text())) 
     pixmap = icon.pixmap(72, 72) 
     icon = QtGui.QIcon(pixmap) 
     item.setIcon(icon) 

它调用线程(当一组图片被丢弃到列表框中它发生)的部分:

self.thread.images.append(f) 

    item = QtGui.QListWidgetItem(f, self.ui.pageList) 
    item.setStatusTip(f) 

    self.thread.start() 

我不知道如何处理这样的东西,因为我只是一个GUI新手;)

感谢所有。

回答

8

经过多次尝试,我终于明白了。我不能在非GUI线程中使用QIconQPixmap,所以我不得不使用QImage,因为它传输的很好。

这里的魔码:从thumbnailer.pyQThread

摘录:

icon = QtGui.QImage(image_file) 
    self.emit(QtCore.SIGNAL('makeIcon(int, QImage)'), i, icon) 

makeIcon()功能:

def makeIcon(self, index, image): 
    item = self.ui.pageList.item(index) 
    pixmap = QtGui.QPixmap(72, 72) 
    pixmap.convertFromImage(image) # <-- This is the magic function! 
    icon = QtGui.QIcon(pixmap) 
    item.setIcon(icon) 

希望这有助于任何人试图让一个图像缩略图线程;)