pyqt5视频小部件和表格小部件并排

时间:2018-04-28 12:45:19

标签: python-3.x pyqt5

我希望将一个表小部件与视频小部件和webview小部件并排放置。要垂直堆叠的视频和webview小部件。 我试图首先将表格小部件和视频小部件并排,但视频小部件被表格小部件隐藏。我能够听到音频,所以视频似乎正在运行,但似乎并没有显示视频部分。 代码有什么问题? 粘贴示例代码。我还没有添加网页小部件。

import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtCore import QDir, Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget, QTableWidget,QVBoxLayout,
    QTableWidgetItem, QLabel, QHBoxLayout,QGridLayout)

class Window(QWidget):  
    def __init__(self,):
        super().__init__()
        table1 = QTableWidget()
        table1.setRowCount(2)
        table1.setColumnCount(2)
        table1.setItem(0,0, QTableWidgetItem("Cell (1,1)"))
        table1.setItem(0,1, QTableWidgetItem("Cell (1,2)"))
        table1.setItem(1,0, QTableWidgetItem("Cell (2,1)"))
        table1.setItem(1,1, QTableWidgetItem("Cell (2,2)"))

        self.VideoWidget = QVideoWidget()

        self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
        self.player.setMedia(QMediaContent(QUrl.fromLocalFile("test.mp4")))
        self.player.play()
        self.player.setVideoOutput(self.VideoWidget)

        self.layout = QHBoxLayout()
        self.layout.addWidget(table1)
        self.layout.addWidget(self.VideoWidget)

        self.setLayout(self.layout) 

        self.move(0,0)
        self.resize(320, 240)

        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()

    app.exec_()

1 个答案:

答案 0 :(得分:0)

我能够通过使用QSplitter解决问题。 代码如下:

import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtCore import QDir, Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget, QTableWidget,QVBoxLayout,
    QTableWidgetItem, QHBoxLayout,QSplitter,QGroupBox)
from PyQt5.QtWebKit import *
from PyQt5.QtWebKitWidgets import *


class Window(QWidget):  
    def __init__(self,):
        super().__init__()

        self.v_layout = QVBoxLayout(self)

        self.splitter = QSplitter(QtCore.Qt.Horizontal)

        self.left = QGroupBox('Left')

        self.table1 = QTableWidget()
        self.table1.setRowCount(2)
        self.table1.setColumnCount(2)
        self.table1.setItem(0,0, QTableWidgetItem("Cell (1,1)"))
        self.table1.setItem(0,1, QTableWidgetItem("Cell (1,2)"))
        self.table1.setItem(1,0, QTableWidgetItem("Cell (2,1)"))
        self.table1.setItem(1,1, QTableWidgetItem("Cell (2,2)"))

        self.left_layout = QVBoxLayout(self.left)
        self.left_layout.addWidget(self.table1)

        self.right = QGroupBox('Right')
        self.VideoWidget = QVideoWidget()

        self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
        self.player.setMedia(QMediaContent(QUrl.fromLocalFile("test.mp4")))
        self.player.play()
        self.player.setVideoOutput(self.VideoWidget)

        self.webview = QWebView()
        url = "https://www.google.com"
        self.webview.load(QUrl(url))

        self.right_layout = QVBoxLayout(self.right)
        self.right_layout.addWidget(self.webview)

        self.right_layout.addWidget(self.VideoWidget)
        self.splitter.addWidget(self.left)
        self.splitter.addWidget(self.right)

        self.v_layout.addWidget(self.splitter)

        self.resize(840,680)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()

    app.exec_()