jijiz | 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 :
- 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 :
- 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_())
|
[B]Merci !!!![/B] ---------------
Mon Feedback: http://forum.hardware.fr/hardwaref [...] m#t2928561
|