当前位置:   article > 正文

PyQT5 实现滑动鼠标滚轮进行图片的缩放_qt滚轮事件图片放大缩小

qt滚轮事件图片放大缩小

 ImageBox是图像绘制类,主要进行图像绘制。

  1. # -*- coding: utf-8 -*-
  2. import sys
  3. from PyQt5 import QtCore, QtGui, QtWidgets
  4. from PyQt5.QtGui import QImageReader
  5. from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QFileDialog, QFrame
  6. from PyQt5.Qt import QPixmap, QPoint, Qt, QPainter, QIcon
  7. from PyQt5.QtCore import QSize
  8. class ImageBox(QWidget):
  9. def __init__(self):
  10. super(ImageBox, self).__init__()
  11. self.img = None
  12. self.scaled_img = None
  13. self.point = QPoint(0, 0)
  14. self.start_pos = None
  15. self.end_pos = None
  16. self.left_click = False
  17. self.scale = 1
  18. def init_ui(self):
  19. self.setWindowTitle("ImageBox")
  20. def set_image(self, img_path):
  21. """
  22. open image file
  23. :param img_path: image file path
  24. :return:
  25. """
  26. # img = QImageReader(img_path)
  27. # img.setScaledSize(QSize(self.size().width(), self.size().height()))
  28. # img = img.read()
  29. self.img = QPixmap(img_path)
  30. self.scaled_img = self.img
  31. self.update()
  32. def paintEvent(self, e):
  33. """
  34. receive paint events
  35. :param e: QPaintEvent
  36. :return:
  37. """
  38. if self.scaled_img:
  39. painter = QPainter()
  40. painter.begin(self)
  41. painter.scale(self.scale, self.scale)
  42. painter.drawPixmap(self.point, self.scaled_img)
  43. painter.end()
  44. def wheelEvent(self, event):
  45. angle = event.angleDelta() / 8 # 返回QPoint对象,为滚轮转过的数值,单位为1/8度
  46. angleY = angle.y()
  47. # 获取当前鼠标相对于view的位置
  48. if angleY > 0:
  49. self.scale *= 1.1
  50. else: # 滚轮下滚
  51. self.scale *= 0.9
  52. self.adjustSize()
  53. self.update()
  54. def mouseMoveEvent(self, e):
  55. """
  56. mouse move events for the widget
  57. :param e: QMouseEvent
  58. :return:
  59. """
  60. if self.left_click:
  61. self.end_pos = e.pos() - self.start_pos
  62. self.point = self.point + self.end_pos
  63. self.start_pos = e.pos()
  64. self.repaint()
  65. def mousePressEvent(self, e):
  66. """
  67. mouse press events for the widget
  68. :param e: QMouseEvent
  69. :return:
  70. """
  71. if e.button() == Qt.LeftButton:
  72. self.left_click = True
  73. self.start_pos = e.pos()
  74. def mouseReleaseEvent(self, e):
  75. """
  76. mouse release events for the widget
  77. :param e: QMouseEvent
  78. :return:
  79. """
  80. if e.button() == Qt.LeftButton:
  81. self.left_click = False
  82. class Ui_Form(QWidget):
  83. def __init__(self,path):
  84. super(Ui_Form,self).__init__()
  85. self.setupUi()
  86. def setupUi(self):
  87. self.scrollArea = QtWidgets.QScrollArea()
  88. self.scrollArea.setGeometry(QtCore.QRect(240, 50, 719, 309))
  89. self.scrollArea.setWidgetResizable(True)
  90. self.scrollArea.setObjectName("scrollArea")
  91. self.scrollAreaWidgetContents = QtWidgets.QWidget()
  92. # 定义一个总布局
  93. self.gridLayout1 = QtWidgets.QVBoxLayout()
  94. self.scrollAreaWidgetContents.setLayout(self.gridLayout1)
  95. # self.box是绘图类
  96. self.box = ImageBox()
  97. self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, self.box.width(), self.box.height()))
  98. self.scrollAreaWidgetContents.setMinimumSize(QtCore.QSize(719, 309))
  99. self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
  100. # 子布局
  101. self.gridLayout = QtWidgets.QGridLayout()
  102. self.gridLayout.setObjectName("gridLayout")
  103. # 将绘图类添加进子布局
  104. self.gridLayout.addWidget(self.box, 0, 0, 1, 1)
  105. self.scrollArea.setWidget(self.scrollAreaWidgetContents)
  106. # 打开文件夹按钮,选择要缩放的图片
  107. self.open_file = QtWidgets.QPushButton('选择图片')
  108. self.open_file.setGeometry(QtCore.QRect(30, 100, 81, 41))
  109. self.open_file.setObjectName("open_file")
  110. self.open_file.clicked.connect(self.open_image)
  111. # 将按钮加入子布局
  112. self.gridLayout.addWidget(self.open_file)
  113. # 将子布局加入总布局
  114. self.gridLayout1.addLayout(self.gridLayout)
  115. self.setMinimumSize(500,500)
  116. # 将总布局设置为当前布局文件
  117. self.setLayout(self.gridLayout1)
  118. def open_image(self):
  119. """
  120. select image file and open it
  121. :return:
  122. """
  123. # img_name, _ = QFileDialog.getOpenFileName(self, "打开图片", "", "All Files(*);;*.jpg;;*.png")
  124. img_name, _ = QFileDialog.getOpenFileName(None, "Open Image File","","All Files(*);;*.jpg;;*.png;;*.jpeg")
  125. # img_name = "icons/Nest_17.png"
  126. # print(img_name)
  127. img = QPixmap(img_name)
  128. # print(img.width(),"+",img.height())
  129. self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, img.width(), img.height()))
  130. self.box.set_image(img_name)
  131. if __name__ == "__main__":
  132. app = QtWidgets.QApplication(sys.argv)
  133. # 外部布局调用
  134. m_ui = QFrame()
  135. m_h_box = QtWidgets.QHBoxLayout()
  136. m_ui.setLayout(m_h_box)
  137. ui = Ui_Form()
  138. ui.setupUi()
  139. # 通过addWidget()方法
  140. m_h_box.addWidget(ui)
  141. m_ui.show()
  142. sys.exit(app.exec_())

参考了,pyqt5实现图片显示、图片放大/缩小(通过滚轮)、图片移动(鼠标拖动)_小郁同学-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/100713
推荐阅读
相关标签
  

闽ICP备14008679号