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

Problem with QGraphicsScene and QVideoWidget in Qt5 (python2 pyside2)



  • Hi!

    Can some one confirm if QVideoWidget is not working when added to a QGraphicsScene?
    When I try to run this code (set the path to some mp4 file) the video is not shown or played.

    import signal
    import sys
    
    from PySide2.QtCore import QUrl
    from PySide2.QtMultimedia import QMediaPlayer, QMediaPlaylist, QMediaContent
    from PySide2.QtMultimediaWidgets import QVideoWidget
    from PySide2.QtWidgets import QGraphicsScene, QGraphicsView, QApplication
    
    PATH='<set a mp4 video path here>'
    
    if __name__ == '__main__':
    	app = QApplication(sys.argv)
    	scene = QGraphicsScene()
    	view = QGraphicsView()
    	view.setScene(scene)
    	view.show()
    	media_player = QMediaPlayer()
    	video_widget = QVideoWidget()
    	current_play_list = QMediaPlaylist()
    	media_player.setVideoOutput(video_widget)
    	media_player.setPlaylist(current_play_list)
    	video_proxy = scene.addWidget(video_widget)
    	# video_widget.show()
    	current_play_list.addMedia(QMediaContent(QUrl.fromLocalFile(PATH)))
    	media_player.play()
    	signal.signal(signal.SIGINT, signal.SIG_DFL)
    	app.exec_()
    
    

    If I just comment the video_proxy = scene.addWidget(video_widget) line and uncomment the video_widget.show() the video is played as an independent widget witout problems. Can someone confirm this and maybe have any suggestion to fix it. I know that QGraphicsVideoItem exists but I wanted to have the playlist facilities that I have with QMediaPlaylist.

    import signal
    import sys
    
    from PySide2.QtCore import QUrl
    from PySide2.QtMultimedia import QMediaPlayer, QMediaPlaylist, QMediaContent
    from PySide2.QtMultimediaWidgets import QVideoWidget
    from PySide2.QtWidgets import QGraphicsScene, QGraphicsView, QApplication
    
    PATH='<set a mp4 video path here>'
    
    if __name__ == '__main__':
    	app = QApplication(sys.argv)
    	scene = QGraphicsScene()
    	view = QGraphicsView()
    	view.setScene(scene)
    	view.show()
    	media_player = QMediaPlayer()
    	video_widget = QVideoWidget()
    	current_play_list = QMediaPlaylist()
    	media_player.setVideoOutput(video_widget)
    	media_player.setPlaylist(current_play_list)
    	# video_proxy = scene.addWidget(video_widget)
    	video_widget.show()
    	current_play_list.addMedia(QMediaContent(QUrl.fromLocalFile(PATH)))
    	media_player.play()
    	signal.signal(signal.SIGINT, signal.SIG_DFL)
    	app.exec_()
    

    Thank you in advance.
    Best.


  • Lifetime Qt Champion

    Hi,

    Based on QTBUG-35299, it can't work because of the use of a native handle for the QVideoWidget.



  • @SGaist thank you so much for your quick answer!



  • If somebody find the same problem than me, it can be partially solved using the QGraphicsVideoItem insted QVideoWidget. You can set the QMediaPlayer setVideoOutput to the QGraphicsVideoItem and everything works fine. You can also have all the QMediaPlaylist functionalities.

    The example code would be something like this:

    import signal
    import sys
    
    from PySide2.QtCore import QUrl
    from PySide2.QtMultimedia import QMediaPlayer, QMediaPlaylist, QMediaContent
    from PySide2.QtMultimediaWidgets import QVideoWidget, QGraphicsVideoItem
    from PySide2.QtWidgets import QGraphicsScene, QGraphicsView, QApplication
    
    PATH='<set a mp4 video path here>'
    
    if __name__ == '__main__':
    	app = QApplication(sys.argv)
    	scene = QGraphicsScene()
    	view = QGraphicsView()
    	view.setScene(scene)
    	view.show()
    	media_player = QMediaPlayer()
    	video_widget = QGraphicsVideoItem()
    	current_play_list = QMediaPlaylist()
    	media_player.setVideoOutput(video_widget)
    	media_player.setPlaylist(current_play_list)
    	video_proxy = scene.addItem(video_widget)
    	video_widget.show()
    	current_play_list.addMedia(QMediaContent(QUrl.fromLocalFile(PATH)))
    	media_player.play()
    	signal.signal(signal.SIGINT, signal.SIG_DFL)
    	app.exec_()
    

Log in to reply