如何在QVideoWidget之上以透明方式绘制QtGraphicsView

时间:2018-12-22 22:25:44

标签: python pyqt pyqt5 qgraphicsview qtmultimedia

我正在尝试在视频播放器(QVideoWidget)上叠加一些图形(QtGraphicsView)。我已经尝试将QtGraphicsView子类样式表设置为透明和背景画笔,但是没有一个起作用。

#self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(30, 30, 30, 3)))
#self.setStyleSheet("background-color:rgba(0,0,0,0)")
#self.setStyleSheet("background:rgba(0,0,0,0)")
self.setStyleSheet("background:transparent")
self.setStyleSheet("background-color:transparent")
self.setStyleSheet("background-color:rgba(30,30,30,3)")
self.setStyleSheet("background:rgba(30,30,30,3)")

实际意图是轻松裁剪视频(视觉方式)。关于捕获事件,进行数学运算等所有其他工作都已完成。此图像很好地说明了情况。pyqt5 ui。在这一点上,感觉确实是我做错了方法,在QtMultiMedia组件中必须有一些更简单的方法可以在它们之上进行绘制。任何想法都非常感谢。

1 个答案:

答案 0 :(得分:1)

一种可能的解决方案是使用QGraphicsVideoItem而不是QVideoWidget并将其嵌入QGraphicsView中,然后可以将其他项设置为QGraphicsVideoItem的子项,使其位于顶部,此外新项的位置将与QGraphicsVideoItem。

import os
from PyQt5 import QtCore, QtGui, QtWidgets, QtMultimedia, QtMultimediaWidgets

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        self._scene = QtWidgets.QGraphicsScene(self)
        self._gv = QtWidgets.QGraphicsView(self._scene)

        self._videoitem = QtMultimediaWidgets.QGraphicsVideoItem()
        self._scene.addItem(self._videoitem)
        self._ellipse_item = QtWidgets.QGraphicsEllipseItem(QtCore.QRectF(50, 50, 40, 40), self._videoitem)
        self._ellipse_item.setBrush(QtGui.QBrush(QtCore.Qt.green))
        self._ellipse_item.setPen(QtGui.QPen(QtCore.Qt.red))

        self._player = QtMultimedia.QMediaPlayer(self, QtMultimedia.QMediaPlayer.VideoSurface)
        self._player.stateChanged.connect(self.on_stateChanged)
        self._player.setVideoOutput(self._videoitem)

        file = os.path.join(os.path.dirname(__file__), "small.mp4")
        self._player.setMedia(QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(file)))
        button = QtWidgets.QPushButton("Play")
        button.clicked.connect(self._player.play)

        self.resize(640, 480)
        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self._gv)
        lay.addWidget(button)

    @QtCore.pyqtSlot(QtMultimedia.QMediaPlayer.State)
    def on_stateChanged(self, state):
        if state == QtMultimedia.QMediaPlayer.PlayingState:
            self._gv.fitInView(self._videoitem, QtCore.Qt.KeepAspectRatio)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

enter image description here