PyQt:如何处理 QThread 中的 QPixmap?
这一定是我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过多次尝试,我终于得到了。我无法在非 GUI 线程中使用
QIcon
或QPixmap
,因此我必须使用QImage
来代替,因为它传输美好的。这是神奇的代码:
摘自
thumbnailer.py
QThread
类:makeIcon()
函数:希望这对尝试制作图像缩略图线程的其他人有所帮助;)
After many attempts, I finally got it. I can't use a
QIcon
orQPixmap
from within a non-GUI thread, so I had to use aQImage
instead, as that transmits fine.Here's the magic code:
Excerpt from the
thumbnailer.py
QThread
class:makeIcon()
function:Hope this helps anyone else trying to make an image thumbnailing thread ;)