以供将来参考,这是我设法使其工作的方法。
使用信号和槽机制,以下应用程序可以工作。信号/槽机制似乎发现在 up_camera_callback 函数内部创建并发送到 CameraDisplay.updateFrame 函数的图像来自不同的线程并采取了必要的预防措施。
class CameraDisplay(QtGui.QLabel):
def __init__(self):
super(CameraDisplay, self).__init__()
def updateFrame(self, image):
self.setPixmap(QtGui.QPixmap.fromImage(image))
class ControlCenter(QtGui.QWidget):
up_camera_signal = QtCore.Signal(QtGui.QImage)
up_camera = None
def __init__(self):
super(ControlCenter, self).__init__()
self.up_camera = CameraDisplay()
self.up_camera_signal.connect(self.up_camera.updateFrame)
grid = QtGui.QGridLayout()
grid.setSpacing(10)
grid.addWidget(self.up_camera, 0, 0)
self.setLayout(grid)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('Control Center')
self.show()
def up_camera_callback(self, data):
'''This function gets called by an external thread'''
try:
image = QtGui.QImage(data.data, data.width, data.height, QtGui.QImage.Format_RGB888)
self.up_camera_signal.emit(image)
except Exception, e:
print(e)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
ex = ControlCenter()
sys.exit(app.exec_())