def mousePressEvent( self, event ): # handle an internal move # start a drag event if event.button() == Qt.LeftButton and self.dragDropRect().contains(event.pos()): # create the pixmap pixmap = QPixmap.grabWidget(self, self.rect()) # create the mimedata mimeData = QMimeData() mimeData.setText('ItemTitle::%s' % (self.title())) # create the drag drag = QDrag(self) drag.setMimeData(mimeData) drag.setPixmap(pixmap) drag.setHotSpot(event.pos()) if not drag.exec_(): self._accordianWidget.emitItemDragFailed(self) event.accept() # determine if the expand/collapse should occur elif event.button() == Qt.LeftButton and self.expandCollapseRect().contains(event.pos()): self._clicked = True event.accept() else: event.ignore()
def setObjPic(self): """加载的是obj模型,设置obj的图片""" pixmap = QPixmap(file_path + r"\res\image\objimg.jpg") self.toolButton.setIcon(pixmap) self.toolButton.setIconSize(self.size())
class ImageFieldWidget(FieldWidget, object): def __init__(self, *args, **kwargs): super(ImageFieldWidget, self).__init__(*args, **kwargs) self._value = '' self._pixmap = None widget = label.BaseLabel(parent=self) widget.setObjectName('widget') self.setStyleSheet('min-height: 32px;') widget.setScaledContents(False) widget.setAlignment(Qt.AlignHCenter) self.set_widget(widget) self.layout().addStretch() # ================================================================================================================= # OVERRIDES # ================================================================================================================= def resizeEvent(self, event): """ Overrides FieldWidget resizeEvent function Called when teh field widget is resized :param event: QResizeEvent """ self.update() def value(self): """ Implements FieldWidget value function Returns the path of the image in disk :return: str """ return self._value def set_value(self, value): """ Overrides FileWidget set_value function Sets the path of the image in disk :param value: str """ self._value = value self._pixmap = QPixmap(value) self.update() def update(self): """ Updates the image depending on the size """ if not self._pixmap: return width = self.widget().height() if self.widget().width() > self.widget().height(): pixmap = self._pixmap.scaledToWidth(width, Qt.SmoothTransformation) else: pixmap = self._pixmap.scaledToHeight(width, Qt.SmoothTransformation) self.widget().setPixmap(pixmap) self.widget().setAlignment(Qt.AlignLeft)
if __name__ == '__main__': import sys MouseCount = 70 app = QApplication(sys.argv) scene = QGraphicsScene() scene.setSceneRect(-300, -300, 600, 600) scene.setItemIndexMethod(QGraphicsScene.NoIndex) for i in range(MouseCount): mouse = Chip(QColor(0.7, 0.4, 0.5)) mouse.setPos( math.sin((i * 6.28) / MouseCount) * 200, math.cos((i * 6.28) / MouseCount) * 200) scene.addItem(mouse) view = QGraphicsView(scene) view.setRenderHint(QPainter.Antialiasing) view.setBackgroundBrush(QBrush(QPixmap(':/cheese.jpg'))) view.setCacheMode(QGraphicsView.CacheBackground) view.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate) view.setDragMode(QGraphicsView.ScrollHandDrag) view.setWindowTitle("Colliding Mice") view.resize(400, 300) view.show() sys.exit(app.exec_())