线程化:在GUI线程外使用像素图是不安全的 [英] threading: It is not safe to use pixmaps outside the GUI thread

查看:107
本文介绍了线程化:在GUI线程外使用像素图是不安全的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个音乐播放器,该音乐播放器使用SqueezeBox控制器应用程序SqueezePlay检查状态.简而言之,我正在使用线程每5秒检查一次Squeezeplay的状态.如果歌曲标题发生更改,我让它更新标签(Qlabel,专辑插图(QPixmap)等).但是,当我要求它通过线程更新它时,我会得到在外部使用像素图并不安全GUI线程.

I'm building a music player, that checks the status with SqueezePlay, which is a SqueezeBox controller app. To cut a long story short, I'm checking the status of Squeezeplay ever 5 seconds by using threading. If the song title changes, I let it update the labels (Qlabel, album artwork (QPixmap), etc. However, when I ask it to update it via threading, I'm getting It is not safe to use pixmaps outside the GUI thread .

如何进行穿线但仍设置QPixmap?

How can I do threading but still set the QPixmap?

示例代码:

#self.sq.getArtwork() returns variable with the image
coverArt = self.sq.getArtwork()
coverPixMap = QtGui.QPixmap()
coverPixMap.loadFromData(coverArt)
self.albumArt.setPixmap(coverPixMap)

非常感谢!

更新: 我在Emit上尝试了以下方法,但没有用,有人可以看看我在做什么错吗?

Update: I tried the following with Emit, but it doesn't work, can someone take a look what I'm doing wrong?

def setNewArtwork(self, image):
    coverPixMap = QtGui.QPixmap()
    coverPixMap.convertFromImage(image)
    icon = QtGui.QIcon(coverPixMap)
    item.setIcon(icon)

def getNewArtwork(self):
    coverArt = self.sq.getArtwork()
    icon = QtGui.QImage(coverArt)
    self.emit(QtCore.SIGNAL('setNewArtwork(QImage)'), icon)

推荐答案

所有图形化Qt操作都应在主线程中进行.确实不允许其他线程调用Qt图形操作(可能包括像素图).

All graphical Qt operations should happen in the main thread. Other threads are not really allowed to call Qt graphical operations (including probably pixmaps).

他们可以向主线程发送Qt信号.或者简单地(在Linux上)写入管道,并让主线程等待输入那个管道.

They could emit Qt signals to the main thread. Or simply (on Linux) write into a pipe, and have the main thread wait for input on that pipe.

当然,您必须定义所需的信号(以及插槽).在C ++代码中,您需要用signals:(或slots:)标记它们,并且您的C ++代码应由

Of course, you have to define the signals (and also the slots) you want. In C++ code, you need to mark them with signals: (or slots:) and your C++ code should be processed by the moc. I don't know what is the Python counterpart (perhaps python reflection abilities might be enough, I really don't know). You then have to connect signals to slots, with a queued connection. I have no idea how to do that in Python.

这篇关于线程化:在GUI线程外使用像素图是不安全的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆