这一定是我与Pyqt遇到的最大滋扰:我已经为我的应用程序砍了一个缩略图(我必须要缩写大量图像),而且看起来它会起作用(并且它可以工作) 几乎 做)。我的主要问题是每当我发送一条错误消息 SIGNAL 从我的线程:

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

我不知道如何解决这个问题。我尝试通过一个 QIcon 通过我的 SIGNAL, ,但这仍然会产生相同的错误。如果有帮助,这里是处理此内容的代码块:

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()

我不确定如何处理这种事情,因为我只是一个新手;)

谢谢大家。

有帮助吗?

解决方案

经过多次尝试,我终于明白了。我不能使用 QIcon 或者 QPixmap 从非gui线程中,我不得不使用 QImage 相反,由于这种传输良好。

这是魔法代码:

摘录 thumbnailer.py QThread 班级:

  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)

希望这有助于其他任何试图制作图像缩略图线程的人;)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top