from PySide6 import QtWidgets, QtGui, QtCore
# class VideoWidget(QtOpenGLWidgets.QOpenGLWidget):
class VideoWidget(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Ignored)
# self.setAlignment(QtCore.Qt.AlignCenter)
self.image_ = None
self.ori_image = None
def set_image(self, image):
self.ori_image = image
self.image_ = self.ori_image.scaled(self.size(), aspectMode=QtCore.Qt.AspectRatioMode.KeepAspectRatio,
mode=QtCore.Qt.FastTransformation)
self.repaint() #立刻刷新,update可能会合并
def resizeEvent(self, event: QtGui.QResizeEvent) -> None:
self.image_ = self.ori_image.scaled(self.size(), aspectMode=QtCore.Qt.AspectRatioMode.KeepAspectRatio,
mode=QtCore.Qt.FastTransformation)
self.update()
def paintEvent(self, event: QtGui.QPaintEvent) -> None:
painter = QtGui.QPainter()
painter.begin(self)
if self.image_ is None:
return
draw_rect = QtCore.QRect(0, 0, self.image_.width(), self.image_.height())
draw_rect.moveCenter(self.rect().center())
painter.fillRect(self.rect(), QtGui.Qt.white)
painter.drawPixmap(draw_rect, self.image_)
painter.end()
标签:QtGui,self,VideoWidget,QtCore,image,rect,Qt
From: https://www.cnblogs.com/kk0716/p/16849989.html