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

Class BigImageView

QListView/ImageView.py:26–138  ·  view source on GitHub ↗

图片查看控件

Source from the content-addressed store, hash-verified

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

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected