| 123 | self._animation.start() |
| 124 | |
| 125 | def setPixmap(self, path): |
| 126 | if not os.path.exists(path): |
| 127 | self._image = "" |
| 128 | self._pixmap = None |
| 129 | return |
| 130 | self._image = path |
| 131 | size = ( |
| 132 | max( |
| 133 | min(self.width(), self.height()), |
| 134 | min(self.minimumWidth(), self.minimumHeight()), |
| 135 | ) |
| 136 | - self._padding |
| 137 | ) # 需要边距的边框 |
| 138 | radius = int(size / 2) |
| 139 | image = QImage(size, size, QImage.Format_ARGB32_Premultiplied) |
| 140 | image.fill(Qt.transparent) # 填充背景为透明 |
| 141 | pixmap = QPixmap(path).scaled( |
| 142 | size, size, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation |
| 143 | ) |
| 144 | # QPainter |
| 145 | painter = QPainter() |
| 146 | painter.begin(image) |
| 147 | painter.setRenderHint(QPainter.Antialiasing, True) |
| 148 | painter.setRenderHint(QPainter.SmoothPixmapTransform, True) |
| 149 | # QPainterPath |
| 150 | path = QPainterPath() |
| 151 | path.addRoundedRect(0, 0, size, size, radius, radius) |
| 152 | # 切割圆 |
| 153 | painter.setClipPath(path) |
| 154 | painter.drawPixmap(0, 0, pixmap) |
| 155 | painter.end() |
| 156 | self._pixmap = QPixmap.fromImage(image) |
| 157 | self.update() |
| 158 | |
| 159 | def pixmap(self): |
| 160 | return self._pixmap |