Displaying a video stream in QLabel with PySide

后端 未结 1 1230
星月不相逢
星月不相逢 2021-01-01 03:28

Can anybody point me in the right direction on how to create a new QMovie \"provider\" in PySide?

I have a video stream that I want to display as simply as possible

1条回答
  •  时光说笑
    2021-01-01 04:03

    For future reference here's how I managed to get this working.

    Using the signals and slot mechanism, the following application works. The signal/slot mechanism seems to figure out that the image that gets created inside the up_camera_callback function and emitted to the CameraDisplay.updateFrame function is coming from a different thread and takes the necessary precautions.

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

    0 讨论(0)
提交回复
热议问题