Qt: A better way to fullscreen/revert QGraphicsView and QVideoWidget?

Viewed 27

I am trying to implement fullscreen and revert functions to my multimedia viewer program, following this thread (and some other similar threads), I've reached a conclusion similar to the following MRE (the actual block of code performing/reverting the fullscreen is the elif event.key() == qtc.Qt.Key.Key_F... block inside keyPressEvent of class Stack):

from __future__ import annotations

from PySide6 import QtCore as qtc
from PySide6 import QtGui as qtg
from PySide6 import QtMultimedia as qtm
from PySide6 import QtMultimediaWidgets as qtmw
from PySide6 import QtWidgets as qtw

image_path = ""
video_path = ""


class Stack(qtw.QStackedWidget):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self._full_screen_widget = None

    def keyPressEvent(self, event: qtg.QKeyEvent) -> None:
        if event.key() == qtc.Qt.Key.Key_N:
            if not self._full_screen_widget:
                if self.currentWidget() == video:
                    self.currentWidget().stop()
                    self.currentWidget().setSource("")
                    self.setCurrentWidget(graphics)
                elif self.currentWidget() == graphics:
                    video.play()
                    self.setCurrentWidget(video)
        elif (
            event.key() == qtc.Qt.Key.Key_F
            and event.modifiers() == qtc.Qt.KeyboardModifier.NoModifier
        ):
            if self._full_screen_widget:
                self._full_screen_widget.showNormal()
                self._full_screen_widget.setWindowFlags(
                    self._full_screen_widget._backup_window_flags
                )
                self._full_screen_widget.setParent(self)
                self.addWidget(self._full_screen_widget)
                self.setCurrentWidget(self._full_screen_widget)
                self._full_screen_widget.setFocus(qtc.Qt.FocusReason.OtherFocusReason)
                self._full_screen_widget = None
            else:
                self._full_screen_widget = self.currentWidget()
                self.removeWidget(self._full_screen_widget)
                self._full_screen_widget.setWindowFlag(qtc.Qt.WindowType.Window)
                self._full_screen_widget.showFullScreen()
        else:
            super().keyPressEvent(event)
            return
        event.accept()
        return


class Graphics(qtw.QGraphicsView):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self._backup_window_flags = self.windowFlags()
        self._scene = qtw.QGraphicsScene()
        self._pixmap_item = qtw.QGraphicsPixmapItem()
        self._pixmap_item.setPixmap(qtg.QPixmap(image_path))
        self._scene.addItem(self._pixmap_item)
        self.setScene(self._scene)

    def keyPressEvent(self, event: qtg.QKeyEvent):
        if event.key() == qtc.Qt.Key.Key_F:
            qtc.QCoreApplication.sendEvent(stack, event)
            event.accept()
            return
        super().keyPressEvent(event)
        return


class Video(qtmw.QVideoWidget):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self._backup_window_flags = self.windowFlags()
        loop = qtc.QEventLoop()
        self._media_player = qtm.QMediaPlayer()
        self._media_player.mediaStatusChanged.connect(loop.exit)
        self._media_player.setVideoOutput(self)
        self._media_player.setSource(video_path)
        loop.exec()

    def play(self):
        self._media_player.play()

    def stop(self):
        self._media_player.stop()

    def setSource(self, source: str):
        self._media_player.setSource(source)

    def keyPressEvent(self, event: qtg.QKeyEvent):
        if event.key() == qtc.Qt.Key.Key_F:
            qtc.QCoreApplication.sendEvent(stack, event)
            event.accept()
            return
        super().keyPressEvent(event)
        return


app = qtw.QApplication()

mw = qtw.QMainWindow()
stack = Stack(parent=mw)
graphics = Graphics(parent=stack)
video = Video(parent=stack)
mw.setCentralWidget(stack)
stack.addWidget(graphics)
stack.addWidget(video)
stack.setCurrentWidget(graphics)
mw.show()

app.exec()

As for why I don't just fullscreen the main window, instead of individual elements, is because the actual program's GUI has other elements to it. Hence, the main window fullscreen would contain elements other than the graphics/video widgets.

0 Answers
Related