Bonjour à tous et à toutes,

Je veux créer Une zone QFrame dans laqulle je rajoute des formes qui doivent supporter le glisser déposer.

Voici comment j'ai procédé :

++Mes objets que je vais pouvoir glisser déplacer dans ma QFrame :
-class ImageDragable(QtGui.QLabel,object)
-class SquareDragable(QtGui.QLabel,object)
-class CubicDragable(QtGui.QLabel,object)
-class SquareBox(QtGui.QLabel,object)

++Ma QFrame :
-class DragWidget(QtGui.QFrame,object)
exemple :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
        dp = SquareDragable(4, 3, "Ben la c'est le nom de la boite", self)
        dp.move(30, 30)
        dp.show()

Lorsque je définis les fonctions dragEnterEvent dropEvent mousePressEvent dans la QFrame tous mes objets sont soummis à ces fonctions et ça marche ! Je souhaiterais définir ces 3 méthodes dans chacun des objets image, cubic, square et suqarebox, seulement ceci ne marche pas...

Voici le code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
from PyQt4 import QtGui, QtCore
import draggableicons_rc, sys
 
# Display a cubic
class SquareBox(QtGui.QLabel,object):    
    def __init__(self, QP1, parent=None):
        super (SquareBox, self).__init__ (parent)
        self.setAcceptDrops(True)
        image = QtGui.QImage(QP1.x(), QP1.y(),
                                     QtGui.QImage.Format_ARGB32_Premultiplied)        
        self.rect = QtCore.QRectF(QtCore.QPointF(0.,0.), QtCore.QSizeF(QP1.x(),QP1.y()))
 
        # Drawing the main rectangle
        painter = QtGui.QPainter()
        painter.begin(image)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setPen(QtCore.Qt.black)
        painter.drawLine(QtCore.QPointF(0,0),QtCore.QPointF(QP1.x(),0))
        painter.drawLine(QtCore.QPointF(QP1.x(),0),QP1)
        painter.drawLine(QtCore.QPointF(0,QP1.y()),QP1)
        painter.drawLine(QtCore.QPointF(0,0),QtCore.QPointF(0,QP1.y()))
        self.setPixmap(QtGui.QPixmap.fromImage(image))
        painter.end()
 
    def dragEnterEvent(self, event):
        if (event.mimeData().hasFormat("application/x-dnditemdata")):
            if (event.source()==self):
                event.setDropAction(QtCore.Qt.MoveAction)
                event.accept()
            else:
                event.acceptProposedAction()
        else:
            event.ignore()
 
    def dropEvent(self, event):     
        if (event.mimeData().hasFormat("application/x-dnditemdata")):
            itemData = event.mimeData().data("application/x-dnditemdata")
            dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.ReadOnly)
            # pixmap = QtGui.QPixmap(item.data(QtCore.Qt.DecorationRole))
            pixmap = QtGui.QPixmap()
            offset = QtCore.QPoint()            
            dataStream >> pixmap >> offset           
            newIcon = QtGui.QLabel(self)
            newIcon.setPixmap(pixmap)
            newIcon.move(event.pos() - offset)
            newIcon.show()
            newIcon.setAttribute(QtCore.Qt.WA_DeleteOnClose)
 
            if (event.source() == self):
                event.setDropAction(QtCore.Qt.MoveAction)
                event.accept()
            else:
                event.acceptProposedAction()
 
        else:
            event.ignore()         
    def mousePressEvent(self,event):
        child = self.childAt(event.pos())
        if (not child):
            return 
 
        pixmap = child.pixmap()
        itemData = QtCore.QByteArray()    
        dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.WriteOnly)
        dataStream << pixmap << QtCore.QPoint(event.pos() - child.pos())
        mimeData = QtCore.QMimeData()
        mimeData.setData("application/x-dnditemdata", itemData)
 
        print "dans la square box"
        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        drag.setPixmap(pixmap)
        drag.setHotSpot(event.pos() - child.pos())      
        tempPixmap = QtGui.QPixmap(pixmap)
        child.setPixmap(tempPixmap)      
        if (drag.start(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction):
            child.close()
        else:
            child.show()
            child.setPixmap(pixmap)  
 
# Display a cubic
class CubicDragable(QtGui.QLabel,object):    
    def __init__(self, QP1, QP2, parent=None):
        super (CubicDragable, self).__init__ (parent)
        self.setAcceptDrops(True)
        # Construct the path
        path = QtGui.QPainterPath()
        path.setFillRule(QtCore.Qt.OddEvenFill)
 
        # Drawing the cubic to gap 2 points             
        if (QP1.x()<QP2.x()):
            # Coordinates of the first control point
            QPc1 = QtCore.QPointF((QP2.x()-QP1.x())/2,QP1.y())
            # Coodinates of the second control point
            QPc2 = QtCore.QPointF((QP2.x()-QP1.x())/2,QP2.y())
        else:
            # Coordinates of the first control point
            QPc1 = QtCore.QPointF((QP1.x()-QP2.x())/2,QP1.y())
            # Coodinates of the second control point
            QPc2 = QtCore.QPointF((QP1.x()-QP2.x())/2,QP2.y())
 
        path.moveTo(QP1)
        path.cubicTo(QPc1,QPc2,QP2)
        path.closeSubpath()
 
        # Area where the path will be drawn
        image = QtGui.QImage(abs(QP1.x()- QP2.x()), abs(QP1.y()- QP2.y()),
                             QtGui.QImage.Format_ARGB32_Premultiplied)
 
        # Start the painting device
        painter = QtGui.QPainter()
        painter.begin(image)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
 
        # Dawing the path of our cubic
        painter.drawPath(path)
        self.setPixmap(QtGui.QPixmap.fromImage(image))
        painter.end()
 
    def dragEnterEvent(self, event):
        if (event.mimeData().hasFormat("application/x-dnditemdata")):
            if (event.source()==self):
                event.setDropAction(QtCore.Qt.MoveAction)
                print "on est dans le dragableWidget"
                event.accept()
            else:
                event.acceptProposedAction()
        else:
            event.ignore()
 
    def dropEvent(self, event):
        if (event.mimeData().hasFormat("application/x-dnditemdata")):
            itemData = event.mimeData().data("application/x-dnditemdata")
            dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.ReadOnly)
            pixmap = QtGui.QPixmap()
            offset = QtCore.QPoint()           
            dataStream >> pixmap >> offset            
            newIcon = QtGui.QLabel(self)
            newIcon.setPixmap(pixmap)
            newIcon.move(event.pos() - offset)
            newIcon.show()
            newIcon.setAttribute(QtCore.Qt.WA_DeleteOnClose)
 
            if (event.source() == self):
                event.setDropAction(QtCore.Qt.MoveAction)
                event.accept()
            else:
                event.acceptProposedAction()   
        else:
            event.ignore() 
 
    def mousePressEvent(self,event):
        child = self.childAt(event.pos())
        if (not child):
            return 
 
        pixmap = child.pixmap()
        itemData = QtCore.QByteArray()    
        dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.WriteOnly)
        dataStream << pixmap << QtCore.QPoint(event.pos() - child.pos())
        mimeData = QtCore.QMimeData()
        mimeData.setData("application/x-dnditemdata", itemData)
        print "entrer dans le cubic dragable"
        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        drag.setPixmap(pixmap)
        drag.setHotSpot(event.pos() - child.pos())
        tempPixmap = QtGui.QPixmap(pixmap)        
        child.setPixmap(tempPixmap)
 
        if (drag.start(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction):
            child.close()
        else:
            child.show()
            child.setPixmap(pixmap)  
 
# Display a square
# Parameters : width, height
class SquareDragable(QtGui.QLabel,object):
    def __init__(self, input, output, text, parent=None):
        super (SquareDragable, self).__init__ (parent)
        #QtGui.QLabel.__init__(self, parent)
        self.setAcceptDrops(True)
        # parameters of the bloc; depends of the number of input and output
 
        # max between input and output variables
        m = max(input,output)
        sizeLittleSquare = 10
 
        # size of the bloc in function on the m
        width = QtCore.QString(text).size()*7
        height = m*30+sizeLittleSquare*input
 
        # QString object from the text parameter
        QStringText = QtCore.QString(text)
 
        image = QtGui.QImage(width+2, height+2,
                             QtGui.QImage.Format_ARGB32_Premultiplied)
        # Color of the background
        self.rect = QtCore.QRectF(QtCore.QPointF(1,1), QtCore.QSizeF(width,height))
 
        # Drawing the main rectangle
        painter = QtGui.QPainter()
        painter.begin(image)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)
        painter.setBrush(QtCore.Qt.white)
        painter.drawRoundRect(self.rect,10,10)
 
        # Displaying a text center on the square
        painter.drawText(self.rect, QtCore.Qt.AlignCenter, QStringText)
        self.setPixmap(QtGui.QPixmap.fromImage(image))
        painter.end()
 
    def dragEnterEvent(self, event):
        if (event.mimeData().hasFormat("application/x-dnditemdata")):
            if (event.source()==self):
                event.setDropAction(QtCore.Qt.MoveAction)
                print "on est dans le dragableWidget"
                event.accept()
            else:
                event.acceptProposedAction()
        else:
            event.ignore()
 
    def dropEvent(self, event):     
        if (event.mimeData().hasFormat("application/x-dnditemdata")):
            itemData = event.mimeData().data("application/x-dnditemdata")
            dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.ReadOnly)
            pixmap = QtGui.QPixmap()
            offset = QtCore.QPoint()
            dataStream >> pixmap >> offset
            newIcon = QtGui.QLabel(self)
            newIcon.setPixmap(pixmap)
            newIcon.move(event.pos() - offset)
            newIcon.show()
            newIcon.setAttribute(QtCore.Qt.WA_DeleteOnClose)
 
 
            if (event.source() == self):
                event.setDropAction(QtCore.Qt.MoveAction)
                event.accept()
            else:
                event.acceptProposedAction()
 
        else:
            event.ignore()         
    def mousePressEvent(self,event):
        child = self.childAt(event.pos())
        if (not child):
            return 
 
        pixmap = child.pixmap()
        itemData = QtCore.QByteArray()    
        dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.WriteOnly)
        dataStream << pixmap << QtCore.QPoint(event.pos() - child.pos())
 
        mimeData = QtCore.QMimeData()
        mimeData.setData("application/x-dnditemdata", itemData)
 
 
        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        drag.setPixmap(pixmap)
        drag.setHotSpot(event.pos() - child.pos())
 
        tempPixmap = QtGui.QPixmap(pixmap)    
        print "entrer dans le square dragable"
        child.setPixmap(tempPixmap)
 
        if (drag.start(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction):
            child.close()
        else:
            child.show()
            child.setPixmap(pixmap)  
 
# Display a picture
# Parameters : path (path of the picture
class ImageDragable(QtGui.QLabel,object):
    def __init__(self, path , parent=None):
        super (ImageDragable, self).__init__ (parent)
        self.setAcceptDrops(True)
        carIcon = QtGui.QImage(path)
        self.setPixmap(QtGui.QPixmap.fromImage(carIcon))
 
    def dragEnterEvent(self, event):
        if (event.mimeData().hasFormat("application/x-dnditemdata")):
            if (event.source()==self):
                event.setDropAction(QtCore.Qt.MoveAction)
                print "on est dans le dragableWidget"
                event.accept()
            else:
                event.acceptProposedAction()
        else:
            event.ignore()
    def dropEvent(self, event):
        if (event.mimeData().hasFormat("application/x-dnditemdata")):
            itemData = event.mimeData().data("application/x-dnditemdata")
            dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.ReadOnly)
            # pixmap = QtGui.QPixmap(item.data(QtCore.Qt.DecorationRole))
            pixmap = QtGui.QPixmap()
            offset = QtCore.QPoint()
            dataStream >> pixmap >> offset
            newIcon = QtGui.QLabel(self)
            newIcon.setPixmap(pixmap)
            newIcon.move(event.pos() - offset)
            newIcon.show()
            newIcon.setAttribute(QtCore.Qt.WA_DeleteOnClose)
 
            if (event.source() == self):
                event.setDropAction(QtCore.Qt.MoveAction)
                event.accept()
            else:
                event.acceptProposedAction()
        else:
            event.ignore()         
    def mousePressEvent(self,event):
        child = self.childAt(event.pos())
        if (not child):
            return 
 
        pixmap = child.pixmap()
        itemData = QtCore.QByteArray()    
        dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.WriteOnly)
        dataStream << pixmap << QtCore.QPoint(event.pos() - child.pos())
 
        mimeData = QtCore.QMimeData()
        mimeData.setData("application/x-dnditemdata", itemData)
        print "entrer image dragable"
 
        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        drag.setPixmap(pixmap)
        drag.setHotSpot(event.pos() - child.pos())
 
        tempPixmap = QtGui.QPixmap(pixmap)                
        child.setPixmap(tempPixmap)
 
        if (drag.start(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction):
            child.close()
        else:
            child.show()
            child.setPixmap(pixmap)  
 
# Frame wich allow to drag and drop objects contained in the frame
class DragWidget(QtGui.QFrame,object):
 
    def __init__(self, parent=None):
        QtGui.QFrame.__init__(self, parent)
        self.setMinimumSize(200, 200)
        self.setFrameStyle(QtGui.QFrame.Sunken | QtGui.QFrame.StyledPanel)
        self.setAcceptDrops(True)
        self.setWindowTitle(self.tr("Objets Dragables"))
 
        # ----------------------------
        # Adding of graphicals objects
        # ----------------------------
        # square
        dp = SquareDragable(4, 3, "Ben la c'est le nom de la boite", self)
        dp.move(30, 30)
        dp.show()
 
        cd = CubicDragable(QtCore.QPointF(0,0),QtCore.QPointF(100,75),self)
        cd.move(10,10)
        cd.show()
 
 
        sb = SquareBox(QtCore.QPointF(100,75),self)
        sb.show()
        # picture        
        id = ImageDragable(":/images/car.png", self)
        id.move(10, 10)
        id.show()
 
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = DragWidget()
    window.show()
    sys.exit(app.exec_())

Merci !!!!