Howto embed a video via mpylayer into a QWidget fr

2019-02-20 03:23发布

问题:

Is it possible in PyQt4 to embed a video via mpylayer into a QWidget (or into a subclass of it). If so, could you provide a minimal working example.

回答1:

For a complete example of a Qt Widget that embeds MPlayer, try qmpwidget.

But here's a minimal PyQt demo to get you started:

import mpylayer
from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.container = QtGui.QWidget(self)
        self.container.setStyleSheet('background: black')
        self.button = QtGui.QPushButton('Open', self)
        self.button.clicked.connect(self.handleButton)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.container)
        layout.addWidget(self.button)
        self.mplayer = mpylayer.MPlayerControl(
            'mplayer', ['-wid', str(self.container.winId())])

    def handleButton(self):
        path = QtGui.QFileDialog.getOpenFileName()
        if not path.isEmpty():
            self.mplayer.loadfile(unicode(path))

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.resize(640, 480)
    window.show()
    sys.exit(app.exec_())

(NB: this demo has only been tested on Linux)



回答2:

You have to get the handle (id) of the widget → http://qt-project.org/doc/qt-4.8/qwidget.html#winId And pass it to the -wid option of the MPlayer.

I can't provide you an example with Qt, simply because I don't know Qt, but I already wrote an MplayerCtrl for wxPython: https://bitbucket.org/dav1d/mplayerctrl

Relevant Code: https://bitbucket.org/dav1d/mplayerctrl/src/c680a1d99ad2/MplayerCtrl.py#cl-873



标签: python pyqt4