[英]QVideoWidget in full-screen mode no longer responds to hotkeys or mouse wheel
在 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)
問題是在窗口中設置了快捷方式,但在QVideoWidget
設置了QVideoWidget
會創建 2 個窗口:原始窗口和QVideoWidget
全屏的窗口。 一種可能的解決方案是在 QVideoWidget 中設置QVideoWidget
或建立QVideoWidget
的上下文是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_())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.