4

考虑修改@ekhumoro 的答案,如下所示。

有一个 mplayer 嵌入在QWidget. 现在我想在视频上叠加一些文字。但是我下面的方法不起作用(标签的背景不透明,以至于只能看到视频和文本)。有什么想法可以解决吗?

更一般地说:如何使透明标签定位在自定义小部件上(在我的情况下是 mplayer-widget)?

如果无法完全实现我想要的,那么只需显示冻结视频的最后一帧(或预定义的帧)并在其上显示一些文本就足够了。

请注意,在稍后阶段,我希望覆盖视频的文本随时间而变化,因此解决方案应该已经考虑到这一点。

对于透明的事情,重要的是要注意我使用的是 linux 环境,这应该特别适用于xmonad.

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)


        self.layout = QtGui.QVBoxLayout(self)
        self.layout.addWidget(self.button)

        self.layout.addWidget(self.container)

        self.mplayer = mpylayer.MPlayerControl(
            'mplayer', ['-wid', str(self.container.winId())])


        self.label = QtGui.QLabel('Some text\n and more',self)
        self.label.move(100,100)
        self.label.setGeometry(200,200,900,300)

        #This doesn't work
        self.label.setAttribute(QtCore.Qt.WA_TranslucentBackground)

        #opacity doesn't work
        self.label.setStyleSheet("QLabel {font-size: 100px; opacity:0.5}")


    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_())

这是我的非工作方法的屏幕截图:

非工作方法

这是我想要伪造的gimp(也许我应该使用红色字体颜色,但这应该只是简单的 css):

假货

编辑 这是我如何尝试使 X.Jacobs 回答适应我的示例。但是它不起作用。只有当我调整窗口大小时,覆盖的文本/行才会在视频上显示一毫秒,然后再次消失(在这两种情况下,如果视频正在运行并且正在暂停)。

import mpylayer
from PyQt4 import QtGui, QtCore

class overlayLabel(QtGui.QLabel):    
    def __init__(self, parent=None):        
        super(overlayLabel, self).__init__(parent)
        self.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter)

        self.setText("OVERLAY TEXT")
        self.setStyleSheet("QLabel {font-size: 100px;}")
        self.setGeometry(200,200,900,300)

class overlay(QtGui.QWidget):    
    def __init__(self, parent=None):        
        super(overlay, self).__init__(parent)

        palette = QtGui.QPalette(self.palette())
        palette.setColor(palette.Background, QtCore.Qt.transparent)

        self.setPalette(palette)

    def paintEvent(self, event):        
        painter = QtGui.QPainter()
        painter.begin(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.fillRect(event.rect(), QtGui.QBrush(QtGui.QColor(255, 255, 255, 27)))
        painter.drawLine(self.width()/8, self.height()/8, 7*self.width()/8, 7*self.height()/8)
        painter.drawLine(self.width()/8, 7*self.height()/8, 7*self.width()/8, self.height()/8)
        painter.setPen(QtGui.QPen(QtCore.Qt.NoPen))        


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)


        self.layout = QtGui.QVBoxLayout(self)
        self.layout.addWidget(self.button)

        self.layout.addWidget(self.container)

        self.mplayer = mpylayer.MPlayerControl(
            'mplayer', ['-wid', str(self.container.winId())])



        ## Both versions don't work:

        #self.label = overlay(self.container)
        self.label = overlayLabel(self.container)



    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_())
4

1 回答 1

3

查看此示例覆盖小部件,您可以根据自己的需要进行调整:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4.QtCore import *
from PyQt4.QtGui import *

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

        palette = QPalette(self.palette())
        palette.setColor(palette.Background, Qt.transparent)

        self.setPalette(palette)

    def paintEvent(self, event):        
        painter = QPainter()
        painter.begin(self)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.fillRect(event.rect(), QBrush(QColor(255, 255, 255, 127)))
        painter.drawLine(self.width()/8, self.height()/8, 7*self.width()/8, 7*self.height()/8)
        painter.drawLine(self.width()/8, 7*self.height()/8, 7*self.width()/8, self.height()/8)
        painter.setPen(QPen(Qt.NoPen))        

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

        self.editor = QTextEdit()
        self.editor.setPlainText("OVERLAY"*100)

        self.button = QPushButton("Toggle Overlay")

        self.verticalLayout = QVBoxLayout(self)
        self.verticalLayout.addWidget(self.editor)
        self.verticalLayout.addWidget(self.button)

        self.overlay = overlay(self.editor)
        self.overlay.hide()

        self.button.clicked.connect(lambda: self.overlay.setVisible(False) if self.overlay.isVisible() else self.overlay.setVisible(True))

    def resizeEvent(self, event):    
        self.overlay.resize(event.size())
        event.accept()

if __name__ == "__main__":
    import  sys

    app = QApplication(sys.argv)
    main = windowOverlay()
    main.show()
    sys.exit(app.exec_())

要覆盖文本,请使用以下内容:

class overlayLabel(QLabel):    
    def __init__(self, parent=None):        
        super(overlayLabel, self).__init__(parent)
        self.setAlignment(Qt.AlignHCenter|Qt.AlignVCenter)

        self.setText("OVERLAY TEXT")
于 2012-12-26T20:40:32.357 回答