赞
踩
ImageBox是图像绘制类,主要进行图像绘制。
- # -*- coding: utf-8 -*-
-
- import sys
- from PyQt5 import QtCore, QtGui, QtWidgets
- from PyQt5.QtGui import QImageReader
- from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QFileDialog, QFrame
- from PyQt5.Qt import QPixmap, QPoint, Qt, QPainter, QIcon
- from PyQt5.QtCore import QSize
-
-
- class ImageBox(QWidget):
- def __init__(self):
- super(ImageBox, self).__init__()
- self.img = None
- self.scaled_img = None
- self.point = QPoint(0, 0)
- self.start_pos = None
- self.end_pos = None
- self.left_click = False
- self.scale = 1
-
- def 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.img
- self.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.1
- else: # 滚轮下滚
- self.scale *= 0.9
- self.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_pos
- self.point = self.point + self.end_pos
- self.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 = True
- self.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 = False
-
- class 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_())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。