MCPcopy
hub / github.com/PyQt5/PyQt / GraphicsView

Class GraphicsView

QGraphicsView/DragGraphics.py:59–91  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

57
58
59class GraphicsView(QGraphicsView):
60
61 def __init__(self, *args, **kwargs):
62 super(GraphicsView, self).__init__(*args, **kwargs)
63 self.setAcceptDrops(True)
64 self._scene = QGraphicsScene(self) # 场景
65 self.setScene(self._scene)
66
67 def dragEnterEvent(self, event):
68 """判断拖入的数据是否支持"""
69 mimeData = event.mimeData()
70 if not mimeData.hasFormat('application/node-items'):
71 event.ignore()
72 return
73
74 event.acceptProposedAction()
75
76 dragMoveEvent = dragEnterEvent
77
78 def dropEvent(self, event):
79 """获取拖拽的数据并绘制对于的图形"""
80 datas = event.mimeData().data('application/node-items')
81 datas = json.loads(datas.data().decode())
82 print('datas:', datas)
83
84 path = os.path.join(os.path.dirname(__file__), 'Data/icons')
85 for name in datas:
86 item = QGraphicsPixmapItem(QPixmap(os.path.join(path, name)))
87 item.setFlags(QGraphicsPixmapItem.ItemIsFocusable |
88 QGraphicsPixmapItem.ItemIsMovable)
89 self._scene.addItem(item)
90 pos = self.mapToScene(event.pos())
91 item.moveBy(pos.x(), pos.y())
92
93
94class DragGraphics(QWidget):

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected