pyqt5视频小组未在布局中显示

时间:2017-01-12 12:04:11

标签: pyqt5

我正在编写一个pyqt5的程序,按下按钮会先循环浏览一些图片,然后循环播放一些视频。

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
import glob
import argparse

import sys

class MainWindow(QMainWindow):
    def __init__(self,args):
        super(MainWindow, self).__init__()
        self.setWindowTitle('Navon test')
        self.setWindowFlags(Qt.FramelessWindowHint)
        # exit option for the menu bar File menu
        self.exit = QAction('Exit', self)
        self.exit.setShortcut('Ctrl+q')
        # message for the status bar if mouse is over Exit
        self.exit.setStatusTip('Exit program')
        # newer connect style (PySide/PyQT 4.5 and higher)
        self.exit.triggered.connect(app.quit)
        self.setWindowIcon(QIcon('icon.ico'))
        self.centralwidget = CentralWidget(args)
        self.setCentralWidget(self.centralwidget)

    def keyPressEvent(self, QKeyEvent):
        if QKeyEvent.key() == Qt.Key_Escape:
            QCoreApplication.instance().quit()
        self.centralwidget.startvid()

class CentralWidget(QWidget):
    def __init__(self,args):
        super(CentralWidget, self).__init__()
        self.layout = QVBoxLayout()
        self.layout.setAlignment(Qt.AlignCenter)
        self.setLayout(self.layout)
        self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
        self.vw = QVideoWidget()
        self.player.setVideoOutput(self.vw)

    def startvid(self):
        self.layout.addWidget(self.vw)
        url= QUrl.fromLocalFile(glob.glob("videos/*")[0])
        content= QMediaContent(url)
        self.player.setMedia(content)
        self.player.setVideoOutput(self.vw)
        self.player.play()
if __name__== "__main__":
    parser = argparse.ArgumentParser()
    #~ parser.add_argument("-nb","--nobox",action="store_true", help="do not wait for the box connection")
    args = parser.parse_args()
    app = QApplication(sys.argv)
    mainwindow = MainWindow(args)
    #~ mainwindow.showFullScreen()
    mainwindow.show()


    sys.exit(app.exec_())

我尝试粘贴最小代码。问题是,我按下没有显示的按钮,虽然我使用了这样的例子PyQt5 - Can't play video using QVideoWidget来测试播放视频是否正常,这些都有效。就好像它没有将小部件添加到布局或其他东西。知道什么可能是错的吗?

1 个答案:

答案 0 :(得分:0)

我不得不使用QGraphicsView来实现我想要的,这是一个修复:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *
import glob
class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setWindowTitle('Navon test')
        self.setWindowFlags(Qt.FramelessWindowHint)
        # exit option for the menu bar File menu
        self.exit = QAction('Exit', self)
        self.exit.setShortcut('Ctrl+q')
        # message for the status bar if mouse is over Exit
        self.exit.setStatusTip('Exit program')
        # newer connect style (PySide/PyQT 4.5 and higher)
        self.exit.triggered.connect(app.quit)
        self.setWindowIcon(QIcon('icon.ico'))
        self.centralwidget = VideoPlayer()
        self.setCentralWidget(self.centralwidget)

    def keyPressEvent(self, QKeyEvent):
        if QKeyEvent.key() == Qt.Key_Escape:
            self.centralwidget.phaseQuit(2)
        self.centralwidget.play()

class VideoPlayer(QWidget):

    def __init__(self, parent=None):
        super(VideoPlayer, self).__init__(parent)
        self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
        self.videoItem = QGraphicsVideoItem()
        self.videoItem.setSize(QSizeF(640, 480))
        scene = QGraphicsScene(self)
        graphicsView = QGraphicsView(scene)
        scene.addItem(self.videoItem)
        layout = QVBoxLayout()
        layout.addWidget(graphicsView)
        self.setLayout(layout)
        self.mediaPlayer.setVideoOutput(self.videoItem)
        self.counter = 0




    def play(self):
        if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
            pass
        else:
            self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(glob.glob("videos/*")[self.counter])))
            self.mediaPlayer.play()
            self.counter += 1


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    player = MainWindow()
    player.show()
    sys.exit(app.exec_())