PyQt:如何处理 QThread 中的 QPixmap?

发布于 2024-10-10 17:43:17 字数 1281 浏览 0 评论 0原文

这一定是我在使用 PyQT 时遇到的最大麻烦:我已经为我的应用程序拼凑了一个缩略图线程(我必须对大量大图像进行缩略图),而且看起来它可以工作(而且它几乎 确实)。我的主要问题是每当我从线程发送 SIGNAL 时都会出现此错误消息:

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 新手;)

谢谢大家。

This has to be the biggest nuisance I've encountered with PyQT: I've hacked together a thumbnailing thread for my application (I have to thumbnail tons of big images), and it looks like it would work (and it almost does). My main problem is this error message whenever I send a SIGNAL from my thread:

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

I can't figure out how to get around this. I've tried passing a QIcon through my SIGNAL, but that still generates the same error. If it helps, here's the code blocks which deal with this stuff:

The Thumbnailer class:

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)

The part which calls the thread (it occurs when a set of images is dropped onto the list box):

  self.thread.images.append(f)

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

  self.thread.start()

I'm not sure how to handle this kind of stuff, as I'm just a GUI newbie ;)

Thanks to all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛清羽墨安 2024-10-17 17:43:17

经过多次尝试,我终于得到了。我无法在非 GUI 线程中使用 QIconQPixmap,因此我必须使用 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)

希望这对尝试制作图像缩略图线程的其他人有所帮助;)

After many attempts, I finally got it. I can't use a QIcon or QPixmap from within a non-GUI thread, so I had to use a QImage instead, as that transmits fine.

Here's the magic code:

Excerpt from the thumbnailer.py QThread class:

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

makeIcon() function:

  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)

Hope this helps anyone else trying to make an image thumbnailing thread ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文