Python 全屏模式下的QVideoWidget不再响应热键或鼠标滚轮
在PySide2中使用QVideoWidget(尽管python部分可能并不重要)。我用QShortcut设置了热键,一切都很好。当我按“F”键进入全屏模式时,它也能工作,但我不能离开。我的热键或鼠标事件处理程序都无法工作。我最终陷入了全屏模式 有没有办法让它即使在全屏模式下也能响应?我用错误的方法创建热键了吗 此示例演示了以下问题:Python 全屏模式下的QVideoWidget不再响应热键或鼠标滚轮,python,qt,pyside2,qvideowidget,qshortcut,Python,Qt,Pyside2,Qvideowidget,Qshortcut,在PySide2中使用QVideoWidget(尽管python部分可能并不重要)。我用QShortcut设置了热键,一切都很好。当我按“F”键进入全屏模式时,它也能工作,但我不能离开。我的热键或鼠标事件处理程序都无法工作。我最终陷入了全屏模式 有没有办法让它即使在全屏模式下也能响应?我用错误的方法创建热键了吗 此示例演示了以下问题: class MainWindow(QWidget): def __init__(self): super().__init__()
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
全屏的窗口。一种可能的解决方案是在QVideoWidget
中设置QShortcut,或者确定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_())
导入操作系统
从PyQt5导入QtCore、QtGui、QtWidgets、QtMultimedia、QtMultimediaWidgets
CURRENT_DIR=os.path.dirname(os.path.realpath(uu文件_uu))
类主窗口(qtwidts.QWidget):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.\u全屏=假
self.movie_display=qtmediaWidgets.QVideoWidget()
self.movie\u handler=qtmedia.QMediaPlayer(
self,qtmedia.QMediaPlayer.VideoSurface
)
self.movie\u handler.setVideoOutput(self.movie\u显示)
layout=qtwidts.QVBoxLayout(self)
layout.addWidget(self.movie\u显示)
qtwidts.QShortcut(
QtGui.QKeySequence(QtCore.Qt.Key_F),
self.movie_显示,
self.toggle_全屏,
)
#或
“”“qtwidts.QShortcut(
QtGui.QKeySequence(QtCore.Qt.Key_F),
自己
self.toggle_全屏,
context=QtCore.Qt.ApplicationShortcut
)"""
file=os.path.join(os.path.dirname(os.path.realpath(_文件__)),“test.webm”)
media=qtmedia.QMediaContent(QtCore.QUrl.fromLocalFile(文件))
self.movie\u handler.setMedia(媒体)
self.movie_handler.play()
def切换_全屏(自):
self.\u全屏=非self.\u全屏
self.movie\u display.setFullScreen(self.\u全屏)
如果名称=“\uuuuu main\uuuuuuuu”:
导入系统
app=qtwidts.QApplication(sys.argv)
w=主窗口()
w、 show()
sys.exit(app.exec_())