ImageBox是图像绘制类,主要进行图像绘制。
# -*- coding: utf-8 -*-import sysfrom PyQt5 import QtCore, QtGui, QtWidgetsfrom PyQt5.QtGui import QImageReaderfrom PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QFileDialog, QFramefrom PyQt5.Qt import QPixmap, QPoint, Qt, QPainter, QIconfrom PyQt5.QtCore import QSizeclass ImageBox(QWidget):def __init__(self):super(ImageBox, self).__init__()self.img = Noneself.scaled_img = Noneself.point = QPoint(0, 0)self.start_pos = Noneself.end_pos = Noneself.left_click = Falseself.scale = 1def init_ui(self):self.setWindowTitle("ImageBox")def set_image(self, img_path):"""open image file:param img_path: image file path:return:"""# img = QImageReader(img_path)# img.setScaledSize(QSize(self.size().width(), self.size().height()))# img = img.read()self.img = QPixmap(img_path)self.scaled_img = self.imgself.update()def paintEvent(self, e):"""receive paint events:param e: QPaintEvent:return:"""if self.scaled_img:painter = QPainter()painter.begin(self)painter.scale(self.scale, self.scale)painter.drawPixmap(self.point, self.scaled_img)painter.end()def wheelEvent(self, event):angle = event.angleDelta() / 8 # 返回QPoint对象,为滚轮转过的数值,单位为1/8度angleY = angle.y()# 获取当前鼠标相对于view的位置if angleY > 0:self.scale *= 1.1else: # 滚轮下滚self.scale *= 0.9self.adjustSize()self.update()def mouseMoveEvent(self, e):"""mouse move events for the widget:param e: QMouseEvent:return:"""if self.left_click:self.end_pos = e.pos() - self.start_posself.point = self.point + self.end_posself.start_pos = e.pos()self.repaint()def mousePressEvent(self, e):"""mouse press events for the widget:param e: QMouseEvent:return:"""if e.button() == Qt.LeftButton:self.left_click = Trueself.start_pos = e.pos()def mouseReleaseEvent(self, e):"""mouse release events for the widget:param e: QMouseEvent:return:"""if e.button() == Qt.LeftButton:self.left_click = Falseclass Ui_Form(QWidget):def __init__(self,path):super(Ui_Form,self).__init__()self.setupUi()def setupUi(self):self.scrollArea = QtWidgets.QScrollArea()self.scrollArea.setGeometry(QtCore.QRect(240, 50, 719, 309))self.scrollArea.setWidgetResizable(True)self.scrollArea.setObjectName("scrollArea")self.scrollAreaWidgetContents = QtWidgets.QWidget()# 定义一个总布局self.gridLayout1 = QtWidgets.QVBoxLayout()self.scrollAreaWidgetContents.setLayout(self.gridLayout1)# self.box是绘图类self.box = ImageBox()self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, self.box.width(), self.box.height()))self.scrollAreaWidgetContents.setMinimumSize(QtCore.QSize(719, 309))self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")# 子布局self.gridLayout = QtWidgets.QGridLayout()self.gridLayout.setObjectName("gridLayout")# 将绘图类添加进子布局self.gridLayout.addWidget(self.box, 0, 0, 1, 1)self.scrollArea.setWidget(self.scrollAreaWidgetContents)# 打开文件夹按钮,选择要缩放的图片self.open_file = QtWidgets.QPushButton('选择图片')self.open_file.setGeometry(QtCore.QRect(30, 100, 81, 41))self.open_file.setObjectName("open_file")self.open_file.clicked.connect(self.open_image)# 将按钮加入子布局self.gridLayout.addWidget(self.open_file)# 将子布局加入总布局self.gridLayout1.addLayout(self.gridLayout)self.setMinimumSize(500,500)# 将总布局设置为当前布局文件self.setLayout(self.gridLayout1)def open_image(self):"""select image file and open it:return:"""# img_name, _ = QFileDialog.getOpenFileName(self, "打开图片", "", "All Files(*);;*.jpg;;*.png")img_name, _ = QFileDialog.getOpenFileName(None, "Open Image File","","All Files(*);;*.jpg;;*.png;;*.jpeg")# img_name = "icons/Nest_17.png"# print(img_name)img = QPixmap(img_name)# print(img.width(),"+",img.height())self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, img.width(), img.height()))self.box.set_image(img_name)if __name__ == "__main__":app = QtWidgets.QApplication(sys.argv)# 外部布局调用m_ui = QFrame()m_h_box = QtWidgets.QHBoxLayout()m_ui.setLayout(m_h_box)ui = Ui_Form()ui.setupUi()# 通过addWidget()方法m_h_box.addWidget(ui)m_ui.show()sys.exit(app.exec_())
参考了,pyqt5实现图片显示、图片放大/缩小(通过滚轮)、图片移动(鼠标拖动)_小郁同学-CSDN博客