1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
from pathlib import Path
import sys
from PySide6.QtCore import QByteArray, QDataStream, QIODevice, QMimeData, QPoint, Qt
from PySide6.QtGui import QColor, QDrag, QPainter, QPixmap
from PySide6.QtWidgets import QApplication, QFrame, QHBoxLayout, QLabel, QWidget
class DragWidget(QFrame):
def __init__(self, parent: QWidget):
super().__init__(parent)
self.setMinimumSize(200, 200)
self.setFrameStyle(QFrame.Sunken | QFrame.StyledPanel)
self.setAcceptDrops(True)
path = Path(__file__).resolve().parent
boat_icon = QLabel(self)
boat_icon.setPixmap(QPixmap(path / "images" / "boat.png"))
boat_icon.move(10, 10)
boat_icon.show()
boat_icon.setAttribute(Qt.WA_DeleteOnClose)
car_icon = QLabel(self)
car_icon.setPixmap(QPixmap(path / "images" / "car.png"))
car_icon.move(100, 10)
car_icon.show()
car_icon.setAttribute(Qt.WA_DeleteOnClose)
house_icon = QLabel(self)
house_icon.setPixmap(QPixmap(path / "images" / "house.png"))
house_icon.move(10, 80)
house_icon.show()
house_icon.setAttribute(Qt.WA_DeleteOnClose)
def dragEnterEvent(self, event):
if event.mimeData().hasFormat("application/x-dnditem_data"):
if event.source() == self:
event.setDropAction(Qt.MoveAction)
event.accept()
else:
event.acceptProposedAction()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasFormat("application/x-dnditem_data"):
if event.source() == self:
event.setDropAction(Qt.MoveAction)
event.accept()
else:
event.acceptProposedAction()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasFormat("application/x-dnditem_data"):
item_data: QByteArray = event.mimeData().data("application/x-dnditem_data")
data_stream = QDataStream(item_data, QIODevice.ReadOnly)
pixmap = QPixmap()
offset = QPoint()
data_stream >> pixmap >> offset
new_icon = QLabel(self)
new_icon.setPixmap(pixmap)
new_icon.move(event.position().toPoint() - offset)
new_icon.show()
new_icon.setAttribute(Qt.WA_DeleteOnClose)
if event.source() == self:
event.setDropAction(Qt.MoveAction)
event.accept()
else:
event.acceptProposedAction()
else:
event.ignore()
def mousePressEvent(self, event):
child: QLabel = self.childAt(event.position().toPoint())
if not child:
return
pixmap = child.pixmap()
item_data = QByteArray()
data_stream = QDataStream(item_data, QIODevice.WriteOnly)
data_stream << pixmap << QPoint(event.position().toPoint() - child.pos())
mime_data = QMimeData()
mime_data.setData("application/x-dnditem_data", item_data)
drag = QDrag(self)
drag.setMimeData(mime_data)
drag.setPixmap(pixmap)
drag.setHotSpot(event.position().toPoint() - child.pos())
# .copy() is important: python is different than c++ in this case
temp_pixmap = pixmap.copy()
with QPainter(temp_pixmap) as painter:
painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127))
child.setPixmap(temp_pixmap)
if drag.exec(Qt.CopyAction | Qt.MoveAction, Qt.CopyAction) == Qt.MoveAction:
child.close()
else:
child.show()
child.setPixmap(pixmap)
if __name__ == "__main__":
app = QApplication(sys.argv)
main_widget = QWidget()
horizontal_layout = QHBoxLayout(main_widget)
horizontal_layout.addWidget(DragWidget(main_widget))
horizontal_layout.addWidget(DragWidget(main_widget))
main_widget.setWindowTitle("Draggable Icons")
main_widget.show()
sys.exit(app.exec())
|