Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Always display frame of QVideoWidget



  • I have a widget called ThumbnailVideoWidget which plays a short video clip with included play button and position slider widgets. I am using PySide2-5.9.0a1.

    My issues are:

    1. Initially no frames are shown in the QVideoWidget, its just a transparent background. Is there a way to initialize a QVideoWidget/QMediaPlayer with a video and display the first frame automatically?

    2. When I click play and then pause, everything plays properly but when I move my mouse off of the play button, the video goes blank/transparent again. The expected behavior would be to stop and keep on the current frame.

    Source code of widget:

    class ThumbnailVideoWidget(QtWidgets.QWidget):
    
        def __init__(self, path, parent=None):
            super(ThumbnailVideoWidget, self).__init__(parent)
            self.setup_ui()
            self.connect_signals()
            self.set_media(path)
    
    
        def setup_ui(self):
            self.media_player = QtMultimedia.QMediaPlayer(self, QtMultimedia.QMediaPlayer.VideoSurface)
            self.w_video = QtMultimediaWidgets.QVideoWidget()
            self.b_play = QtWidgets.QPushButton()
            self.b_play.setEnabled(False)
            self.b_play.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_MediaPlay))
            self.position_slider = QtWidgets.QSlider(QtCore.Qt.Horizontal)
            self.position_slider.setRange(0, 0)
    
            self.media_player.setVideoOutput(self.w_video)
            self.lyt_main = QtWidgets.QVBoxLayout()
            self.lyt_main.addWidget(self.w_video)
            self.lyt_main.addWidget(self.b_play)
            self.lyt_main.addWidget(self.position_slider)
            self.setLayout(self.lyt_main)
    
    
        def connect_signals(self):
            self.position_slider.sliderMoved.connect(self.set_position)
            self.b_play.clicked.connect(self.play)
            self.media_player.stateChanged.connect(self.media_state_changed)
            self.media_player.positionChanged.connect(self.position_changed)
            self.media_player.durationChanged.connect(self.duration_changed)
    
        
        def set_media(self, path):
            self.media_player.setMedia(QtCore.QUrl.fromLocalFile(path))
            self.b_play.setEnabled(True)
    
    
        def play(self):
            if self.media_player.state() == QtMultimedia.QMediaPlayer.PlayingState:
                self.media_player.pause()
            else:
                self.media_player.play()
    
    
        def set_position(self, position):
            self.media_player.setPosition(position)
    
    
        def media_state_changed(self, state):
            if self.media_player.state() == QtMultimedia.QMediaPlayer.PlayingState:
                self.b_play.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_MediaPause))
            else:
                self.b_play.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_MediaPlay))
    
    
        def position_changed(self, position):
            self.position_slider.setValue(position)
    
    
        def duration_changed(self, duration):
            self.position_slider.setRange(0, duration)
            interval = int(float(duration) / 10.0)
            self.media_player.setNotifyInterval(interval)
    
    
    

  • Lifetime Qt Champion

    Hi,

    This version of PySide2 being pretty old, I would recommend updating before digging further.

    Also, what OS are you running that on ?
    How did you install PySide2 ?



  • @SGaist We built it from source a long long time ago. I'm on Windows 64-bit. I'll try upgrading PySide2 real quick and see if that fixes the issue.



  • @SGaist Ah you know I think we would need to rebuild python27 due to MSVC for our tools. Maybe I will just try going python3/PyQt5 route and get back to you.


  • Lifetime Qt Champion

    You can install it using pip, no need to build it from scratch.



  • @SGaist The pip install for PySide2 only seems to work with python 3.


  • Lifetime Qt Champion

    Ah yes, it seems that there's indeed no Py27 per-built package for Windows.



  • @SGaist Just got back into this, upgrading to the latest PyQt5 w/ Python3 solved the issue.


  • Lifetime Qt Champion

    Great !

    The please mark the thread as solved using the "Topic Tools" button or the three doted menu beside the answer you deem correct so that other forum users may know a solution has been found :-)