Skip to content Skip to sidebar Skip to footer

Overlay Video With Custom Graphics Using Phonon & Pyqt

I am building a eye-tracking data visualization tool using PyQt4 and Phonon module. Essentially, I have a video that a subject was watching while the subject's eye movements were t

Solution 1:

As you comment an option is to use QGraphicsProxyWidget, you can create an object of that type or you can use addWidget:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.phonon import Phonon

class Widget(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        lay = QVBoxLayout(self)
        vp = Phonon.VideoPlayer()
        media = Phonon.MediaSource('/path/of/video')
        vp.load(media)
        vp.play()
        scene = QGraphicsScene()
        self.view = QGraphicsView(scene, self)
        lay.addWidget(self.view)
        proxy = scene.addWidget(vp)
        # or 
        # proxy = QGraphicsProxyWidget()
        # scene.addItem(proxy)
        self.item = scene.addEllipse(QRectF(0, 0, 20, 20), QPen(Qt.red), QBrush(Qt.green))
        self.item.setParentItem(proxy)

    def mousePressEvent(self, event):
        p = self.view.mapToScene(event.pos())
        # move item
        self.item.setPos(p-QPoint(20, 20))
        QWidget.mousePressEvent(self, event)

    def resizeEvent(self, event):
        if event.oldSize().isValid():
            print(self.view.scene().sceneRect())
            self.view.fitInView(self.view.scene().sceneRect(), Qt.KeepAspectRatio)
        QWidget.resizeEvent(self, event)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

Output:

enter image description here

Post a Comment for "Overlay Video With Custom Graphics Using Phonon & Pyqt"