Search code examples
pythonqtpyside2qvideowidgetqshortcut

QVideoWidget in full-screen mode no longer responds to hotkeys or mouse wheel


Using QVideoWidget in PySide2 (although the python part may not be significant). I've set up my hotkeys using QShortcut, and it all works great. When I press 'F' to enter full-screen mode that works too, but then I can't leave. None of my hotkeys or mouse event handlers work. I end up stuck in full-screen mode.

Is there a way to allow it to respond even in full-screen mode? Have I gone about creating my hotkeys the wrong way?

This example demonstrates the issue:

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self._fullscreen = False
        self.movie_display = QVideoWidget(self)
        self.movie_handler = QMediaPlayer()
        self.movie_handler.setVideoOutput(self.movie_display)
        layout = QVBoxLayout()
        layout.addWidget(self.movie_display)
        self.setLayout(layout)
        QShortcut(QKeySequence(QtConsts.Key_F), self, self.toggle_fullscreen)
        s = 'test.webm'
        s = os.path.join(os.path.dirname(__file__), s)
        local = QUrl.fromLocalFile(s)
        media = QMediaContent(local)
        self.movie_handler.setMedia(media)
        self.movie_handler.play()

    def toggle_fullscreen(self):
        self._fullscreen = not self._fullscreen
        self.movie_display.setFullScreen(self._fullscreen)

Solution

  • The problem is that the shortcut is set in the window but when full-screen is set in the QVideoWidget 2 windows are created: The original window and the window where the QVideoWidget is in full-screen. One possible solution is to set the QShortcut in the QVideoWidget or establish that the context of the QShortcut is Qt::ApplicationShortcut:

    import os
    
    from PyQt5 import QtCore, QtGui, QtWidgets, QtMultimedia, QtMultimediaWidgets
    
    CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
    
    
    class MainWindow(QtWidgets.QWidget):
        def __init__(self):
            super().__init__()
            self._fullscreen = False
            self.movie_display = QtMultimediaWidgets.QVideoWidget()
    
            self.movie_handler = QtMultimedia.QMediaPlayer(
                self, QtMultimedia.QMediaPlayer.VideoSurface
            )
            self.movie_handler.setVideoOutput(self.movie_display)
    
            layout = QtWidgets.QVBoxLayout(self)
            layout.addWidget(self.movie_display)
            QtWidgets.QShortcut(
                QtGui.QKeySequence(QtCore.Qt.Key_F),
                self.movie_display,
                self.toggle_fullscreen,
            )
            # or
            """QtWidgets.QShortcut(
                QtGui.QKeySequence(QtCore.Qt.Key_F),
                self,
                self.toggle_fullscreen,
                context=QtCore.Qt.ApplicationShortcut
            )"""
    
            file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test.webm")
            media = QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(file))
            self.movie_handler.setMedia(media)
            self.movie_handler.play()
    
        def toggle_fullscreen(self):
            self._fullscreen = not self._fullscreen
            self.movie_display.setFullScreen(self._fullscreen)
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        w = MainWindow()
        w.show()
        sys.exit(app.exec_())