QtMediaPlayer在无框架和半透明背景PyQt5上不起作用
问题描述
我正在用QMediaPlayer制作一个视频播放器,但它在无边框和半透明的背景窗口上不起作用。我想做一个圆角窗口,所以我需要无边框和半透明的窗口。 以下是我的代码:
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import QApplication,QMainWindow,QFrame
import sys
class Player(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("PyQt Video Player Widget Example")
self.resize(600,400)
self.frame=QFrame(self)
self.frame.setStyleSheet('background:grey;border-radius:20px;')
self.setCentralWidget(self.frame)
#self.setWindowFlag(Qt.FramelessWindowHint)
#self.setAttribute(Qt.WA_TranslucentBackground)
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
videoWidget = QVideoWidget(self.frame)
videoWidget.setGeometry(10,10,580,380)
self.resize(600,400)
self.mediaPlayer.error.connect(self.handleError)
self.mediaPlayer.setVideoOutput(videoWidget)
self.mediaPlayer.setMedia(
QMediaContent(QUrl.fromLocalFile("C:/Users/mishra/Desktop/HiddenfilesWindow/10000000_1874628825927192_6229658593205944320_n(1).mp4")))
self.mediaPlayer.play()
def handleError(self):
print("Error: " + self.mediaPlayer.errorString())
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = Player()
window.show()
sys.exit(app.exec_())
设置半透明背景后,它只播放音频而不播放视频。有人知道如何修复它吗?
解决方案
试用:
import sys
from PyQt5.QtCore import Qt, QUrl, QRectF
from PyQt5.QtGui import QPainterPath, QRegion
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import QApplication, QMainWindow, QFrame, QWidget, QHBoxLayout
class Player(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("PyQt Video Player Widget Example")
self.resize(600,400)
self.frame= QFrame(self)
# self.frame.setStyleSheet('background:grey; border-radius: 20px;')
self.setStyleSheet("Player {background: #000;}") # +++
self.setCentralWidget(self.frame)
# self.setWindowFlag(Qt.FramelessWindowHint)
# self.setAttribute(Qt.WA_TranslucentBackground)
layout = QHBoxLayout(self.frame) # +++
videoWidget = QVideoWidget() # +++
layout.addWidget(videoWidget) # +++
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
# videoWidget = QVideoWidget(self.frame)
# videoWidget.setGeometry(10,10,580,380)
# self.resize(600,400)
self.mediaPlayer.error.connect(self.handleError)
self.mediaPlayer.setVideoOutput(videoWidget)
self.mediaPlayer.setMedia(
QMediaContent(QUrl.fromLocalFile("Samonastrojka.avi")))
self.mediaPlayer.play()
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
def resizeEvent(self, event):
path = QPainterPath()
path.addRoundedRect(QRectF(self.rect()), 20, 20)
reg = QRegion(path.toFillPolygon().toPolygon())
self.setMask(reg)
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
def handleError(self):
print("Error: " + self.mediaPlayer.errorString())
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Player()
window.show()
sys.exit(app.exec_())
相关文章