1

在 PySide2 中使用 QVideoWidget(尽管 python 部分可能并不重要)。我已经使用 QShortcut 设置了我的热键,一切都很好。当我按“F”进入全屏模式时,它也可以工作,但是我不能离开。我的热键或鼠标事件处理程序都不起作用。我最终陷入全屏模式。

有没有办法让它在全屏模式下也能响应?我是否以错误的方式创建热键?

这个例子演示了这个问题:

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)
4

1 回答 1

2

问题是在窗口中设置了快捷方式,但是当在 2 个窗口中设置全屏时,QVideoWidget会创建两个窗口:原始窗口和QVideoWidget全屏窗口。一种可能的解决方案是设置 QShortcutQVideoWidget或确定 QShortcut 的上下文是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_())
于 2020-02-28T07:29:28.623 回答