MCPcopy Index your code
hub / github.com/PyQt5/PyQt / ImageView

Class ImageView

QGraphicsView/ImageView.py:25–137  ·  view source on GitHub ↗

图片查看控件

Source from the content-addressed store, hash-verified

23
24
25class ImageView(QGraphicsView):
26 """图片查看控件"""
27
28 def __init__(self, *args, **kwargs):
29 image = kwargs.pop('image', None)
30 background = kwargs.pop('background', None)
31 super(ImageView, self).__init__(*args, **kwargs)
32 self.setCursor(Qt.OpenHandCursor)
33 self.setBackground(background)
34 self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
35 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
36 self.setRenderHints(QPainter.Antialiasing | QPainter.HighQualityAntialiasing |
37 QPainter.SmoothPixmapTransform)
38 self.setCacheMode(self.CacheBackground)
39 self.setViewportUpdateMode(self.SmartViewportUpdate)
40 self._item = QGraphicsPixmapItem() # 放置图像
41 self._item.setFlags(QGraphicsPixmapItem.ItemIsFocusable |
42 QGraphicsPixmapItem.ItemIsMovable)
43 self._scene = QGraphicsScene(self) # 场景
44 self.setScene(self._scene)
45 self._scene.addItem(self._item)
46 rect = QApplication.instance().desktop().availableGeometry(self)
47 self.resize(int(rect.width() * 2 / 3), int(rect.height() * 2 / 3))
48
49 self.pixmap = None
50 self._delta = 0.1 # 缩放
51 self.setPixmap(image)
52
53 def setBackground(self, color):
54 """设置背景颜色
55 :param color: 背景颜色
56 :type color: QColor or str or GlobalColor
57 """
58 if isinstance(color, QColor):
59 self.setBackgroundBrush(color)
60 elif isinstance(color, (str, Qt.GlobalColor)):
61 color = QColor(color)
62 if color.isValid():
63 self.setBackgroundBrush(color)
64
65 def setPixmap(self, pixmap, fitIn=True):
66 """加载图片
67 :param pixmap: 图片或者图片路径
68 :param fitIn: 是否适应
69 :type pixmap: QPixmap or QImage or str
70 :type fitIn: bool
71 """
72 if isinstance(pixmap, QPixmap):
73 self.pixmap = pixmap
74 elif isinstance(pixmap, QImage):
75 self.pixmap = QPixmap.fromImage(pixmap)
76 elif isinstance(pixmap, str) and os.path.isfile(pixmap):
77 self.pixmap = QPixmap(pixmap)
78 else:
79 return
80 self._item.setPixmap(self.pixmap)
81 self._item.update()
82 self.setSceneDims()

Callers 1

ImageView.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected