Skip to content

Commit 5db4b74

Browse files
committed
DragGraphics demo #180
1 parent ce4226b commit 5db4b74

14 files changed

+159
-675
lines changed

LICENSE

-674
This file was deleted.

QGraphicsView/Data/icons/basic.png

784 Bytes
Loading

QGraphicsView/Data/icons/business.png

913 Bytes
Loading
946 Bytes
Loading

QGraphicsView/Data/icons/dataBase.png

885 Bytes
Loading
1.07 KB
Loading

QGraphicsView/Data/icons/network.png

735 Bytes
Loading

QGraphicsView/Data/icons/science.png

976 Bytes
Loading
472 Bytes
Loading
421 Bytes
Loading

QGraphicsView/DragGraphics.py

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on 2023/02/09
5+
@author: Irony
6+
@site: https://pyqt.site https://github.com/PyQt5
7+
@email: 892768447@qq.com
8+
@file: DragGraphics.py
9+
@description:
10+
"""
11+
12+
import json
13+
import os
14+
15+
try:
16+
from PyQt5.QtCore import QMimeData, Qt
17+
from PyQt5.QtGui import QDrag, QIcon, QPixmap
18+
from PyQt5.QtWidgets import (QApplication, QGraphicsPixmapItem,
19+
QGraphicsScene, QGraphicsView, QHBoxLayout,
20+
QListWidget, QListWidgetItem, QTreeWidget,
21+
QTreeWidgetItem, QWidget)
22+
except ImportError:
23+
from PySide2.QtCore import QMimeData, Qt
24+
from PySide2.QtGui import QDrag, QIcon, QPixmap
25+
from PySide2.QtWidgets import (QApplication, QGraphicsPixmapItem,
26+
QGraphicsScene, QGraphicsView, QHBoxLayout,
27+
QListWidget, QListWidgetItem, QTreeWidget,
28+
QTreeWidgetItem, QWidget)
29+
30+
31+
class ListWidget(QListWidget):
32+
33+
def __init__(self, *args, **kwargs):
34+
super(ListWidget, self).__init__(*args, **kwargs)
35+
self.setDragEnabled(True)
36+
self.setDragDropMode(QListWidget.DragOnly)
37+
self.setDefaultDropAction(Qt.IgnoreAction)
38+
self.setEditTriggers(QListWidget.NoEditTriggers)
39+
self.setResizeMode(QListWidget.Adjust)
40+
self.setViewMode(QListWidget.IconMode)
41+
42+
def startDrag(self, supportedActions):
43+
items = self.selectedItems()
44+
if not items:
45+
return
46+
# 这里就简单的根据名字提示来传递数据了,实际上可以传递任意数据
47+
data = QMimeData()
48+
data.setData('application/node-items',
49+
json.dumps([item.toolTip() for item in items]).encode())
50+
# 这里简单显示第一个缩略图
51+
pixmap = items[0].icon().pixmap(36, 36)
52+
drag = QDrag(self)
53+
drag.setMimeData(data)
54+
drag.setPixmap(pixmap)
55+
drag.setHotSpot(pixmap.rect().center())
56+
drag.exec_(supportedActions)
57+
58+
59+
class 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+
94+
class DragGraphics(QWidget):
95+
96+
def __init__(self, *args, **kwargs):
97+
super(DragGraphics, self).__init__(*args, **kwargs)
98+
self.resize(800, 600)
99+
layout = QHBoxLayout(self)
100+
101+
# 左侧树形控制
102+
self.treeWidget = QTreeWidget(self)
103+
self.treeWidget.header().setVisible(False)
104+
self.treeWidget.setMaximumWidth(300)
105+
layout.addWidget(self.treeWidget)
106+
107+
# 右侧图形显示
108+
self.graphicsView = GraphicsView(self)
109+
layout.addWidget(self.graphicsView)
110+
111+
self._init_trees()
112+
113+
def _init_trees(self):
114+
"""初始化树形控件中的图形节点列表"""
115+
# 1. 获取所有图标
116+
path = os.path.join(os.path.dirname(__file__), 'Data/icons')
117+
icons = [os.path.join(path, name) for name in os.listdir(path)]
118+
119+
# 2. 添加根节点
120+
for i in range(2):
121+
item = QTreeWidgetItem(self.treeWidget)
122+
item.setText(0, 'View %d' % i)
123+
124+
# 3. 添加子节点作为容器用于存放图标
125+
itemc = QTreeWidgetItem(item)
126+
child = ListWidget(self.treeWidget)
127+
self.treeWidget.setItemWidget(itemc, 0, child)
128+
129+
# 4. 添加图标
130+
for icon in icons:
131+
item = QListWidgetItem(child)
132+
item.setIcon(QIcon(icon))
133+
item.setToolTip(os.path.basename(icon))
134+
135+
self.treeWidget.expandAll()
136+
137+
138+
if __name__ == '__main__':
139+
import cgitb
140+
import sys
141+
142+
cgitb.enable(format='text')
143+
app = QApplication(sys.argv)
144+
w = DragGraphics()
145+
w.show()
146+
sys.exit(app.exec_())

QGraphicsView/README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- [绘制世界地图](#1绘制世界地图)
55
- [添加QWidget](#2添加QWidget)
66
- [图片查看器](#3图片查看器)
7+
- [图标拖拽](#4图标拖拽)
78

89
## 1、绘制世界地图
910
[运行 WorldMap.py](WorldMap.py)
@@ -25,4 +26,14 @@
2526

2627
支持放大缩小和移动
2728

28-
![ImageView](ScreenShot/ImageView.gif)
29+
![ImageView](ScreenShot/ImageView.gif)
30+
31+
## 3、图标拖拽
32+
[运行 DragGraphics.py](DragGraphics.py)
33+
34+
该示例主要是包含左侧树状图标列表和右侧视图显示,从左侧拖拽到右侧
35+
36+
1. 重写`QListWidget``startDrag`函数用来封装拖拽数据
37+
2. 重写`QGraphicsView``dragEnterEvent``dragMoveEvent``dropEvent`函数用来处理拖拽事件
38+
39+
![DragGraphics](ScreenShot/DragGraphics.gif)
221 KB
Loading

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
- [绘制世界地图](QGraphicsView/WorldMap.py)
132132
- [添加QWidget](QGraphicsView/AddQWidget.py)
133133
- [图片查看器](QGraphicsView/ImageView.py)
134+
- [图标拖拽](QGraphicsView/DragGraphics.py)
134135
- [QCalendarWidget](QCalendarWidget)
135136
- [QSS美化日历样式](QCalendarWidget/CalendarQssStyle.py)
136137
- [QLCDNumber](QLCDNumber)

0 commit comments

Comments
 (0)