先上一张代码实现的效果图,代码实现了视频播放功能、停止、暂停、前进、后退以及声音的调大调小。视频的图像显示我使用的是QLabel控件,我对他进行了二次封装,以实现图片的自适用缩放,图片的切换等。另外初始图显示我的是我的家乡夜景图,号称“北方水城”的一座历史文化城市,欢迎大家来我的家乡参观游玩。
1、关于pyside6的桌面布局,大家可以从网上搜一下相关内容。介绍一下我的经验,如有不对的地方请大家指正。QtDesigner为我们提供了方便的页面设计功能,类似以前的VB。如下图:
我的主要面继承自QMainWindow类,所以他包含了三部分,自上而下:menubar(菜单)、centralwidget(主功能区)、statusbar(状态区)。
在设计桌面模板前,先有一个大致的规划,这样更方便之后的布局设置。PyQt5中常用的主窗体有两种QMainWindow和QDialog,他们也都继承自QWidget,所以这三个类都可以用来创建主窗口,可以直接使用,也可以继承后再使用。
我这里就是继承了QMainWindow类。下面的代码时通过Compile Form工具将设计的模板文件自动生成的代码,如下:
注意:from qframelesswindow import QMainWindow ,我这里使用了qframelesswindow 中的类
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'video_MainWindow.ui'
##
## Created by: Qt User Interface Compiler version 6.7.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QAction, QBrush, QColor, QConicalGradient,
QCursor, QFont, QFontDatabase, QGradient,
QIcon, QImage, QKeySequence, QLinearGradient,
QPainter, QPalette, QPixmap, QRadialGradient,
QTransform)
from PySide6.QtWidgets import (QApplication, QFrame, QHBoxLayout, QLayout,
QListView, QMainWindow, QMenu, QMenuBar,
QSizePolicy, QStatusBar, QVBoxLayout, QWidget)
from qfluentwidgets import (BodyLabel, PushButton, RoundMenu, Slider)
from qframelesswindow import QMainWindow
import com.sunway.video.ui.image_rc
class Ui_VideoMainWindow(QMainWindow):
def setupUi(self, VideoMainWindow):
if not VideoMainWindow.objectName():
VideoMainWindow.setObjectName(u"VideoMainWindow")
VideoMainWindow.resize(1380, 698)
icon = QIcon()
icon.addFile(u":/icons/raspberry.jpg", QSize(), QIcon.Normal, QIcon.Off)
VideoMainWindow.setWindowIcon(icon)
self.openDir = QAction(VideoMainWindow)
self.openDir.setObjectName(u"openDir")
self.openFile = QAction(VideoMainWindow)
self.openFile.setObjectName(u"openFile")<