By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

Drawing QLabel into QFrame and overlying dragEnterEvent dropEvent and mousePressEven

P: 1
Hi everyone,

My english is quite poor... I prefer inform you about it...

So i would like to draw some pictures (cubic, square, lines) into objects (QLabel), and instanciate those objects into a QFrame. It works, but i would like to make my objects (cubic...) movable, so i want to redefines dragEnterEvent dropEvent and mousePressEven into each of objects wich are QLabel, and it doesn't work.

I thought i could write the dragEnterEvent dropEvent and mousePressEven functions into the QFrame and put a different mime type for cubic, square and lines in order to do specific treatment for each drawable objects but it is piggy.

Can you help me to redefines correctly dragEnterEvent dropEvent and mousePressEven into each of the QLabel objects?

Thank you.

Here the code :

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. # Display a cubic
  4. class SquareBox(QtGui.QLabel, object):    
  5.     def __init__(self, QP1, parent=None):
  6.         super (SquareBox, self).__init__ (parent)
  7.  
  8.         itemData = QtCore.QByteArray()    
  9.         mimeData = QtCore.QMimeData()
  10.         mimeData.setData("application/x-dnditemdata", itemData)
  11.  
  12.  
  13.         image = QtGui.QImage(QP1.x(), QP1.y(),
  14.                                      QtGui.QImage.Format_ARGB32_Premultiplied)        
  15.         self.rect = QtCore.QRectF(QtCore.QPointF(0.,0.), QtCore.QSizeF(QP1.x(),QP1.y()))
  16.  
  17.         # Drawing the main rectangle
  18.         painter = QtGui.QPainter()
  19.         painter.begin(image)
  20.         painter.setRenderHint(QtGui.QPainter.Antialiasing)
  21.         painter.setPen(QtCore.Qt.black)
  22.         painter.drawLine(QtCore.QPointF(0,0),QtCore.QPointF(QP1.x(),0))
  23.         painter.drawLine(QtCore.QPointF(QP1.x(),0),QP1)
  24.         painter.drawLine(QtCore.QPointF(0,QP1.y()),QP1)
  25.         painter.drawLine(QtCore.QPointF(0,0),QtCore.QPointF(0,QP1.y()))
  26.         self.setPixmap(QtGui.QPixmap.fromImage(image))
  27.         painter.end()
  28.  
  29.     def dragEnterEvent(self, event):
  30.         if (event.mimeData().hasFormat("application/x-dnditemdata")):
  31.             if (event.source()==self):
  32.                 event.setDropAction(QtCore.Qt.MoveAction)
  33.                 event.accept()
  34.             else:
  35.                 event.acceptProposedAction()
  36.         else:
  37.             event.ignore()
  38.  
  39.     def dropEvent(self, event):     
  40.         if (event.mimeData().hasFormat("application/x-dnditemdata")):
  41.             itemData = event.mimeData().data("application/x-dnditemdata")
  42.             dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.ReadOnly)
  43.             # pixmap = QtGui.QPixmap(item.data(QtCore.Qt.DecorationRole))
  44.             pixmap = QtGui.QPixmap()
  45.             offset = QtCore.QPoint()            
  46.             dataStream >> pixmap >> offset           
  47.             newIcon = QtGui.QLabel(self)
  48.             newIcon.setPixmap(pixmap)
  49.             newIcon.move(event.pos() - offset)
  50.             newIcon.show()
  51.             newIcon.setAttribute(QtCore.Qt.WA_DeleteOnClose)
  52.  
  53.             if (event.source() == self):
  54.                 event.setDropAction(QtCore.Qt.MoveAction)
  55.                 event.accept()
  56.             else:
  57.                 event.acceptProposedAction()
  58.  
  59.         else:
  60.             event.ignore()         
  61.     def mousePressEvent(self,event):
  62.         child = self.childAt(event.pos())
  63.         if (not child):
  64.             return 
  65.  
  66.         pixmap = child.pixmap()
  67.         itemData = QtCore.QByteArray()    
  68.         dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.WriteOnly)
  69.         dataStream << pixmap << QtCore.QPoint(event.pos() - child.pos())
  70.         mimeData = QtCore.QMimeData()
  71.         mimeData.setData("application/x-dnditemdata", itemData)
  72.  
  73.         print "dans la square box"
  74.         drag = QtGui.QDrag(self)
  75.         drag.setMimeData(mimeData)
  76.         drag.setPixmap(pixmap)
  77.         drag.setHotSpot(event.pos() - child.pos())      
  78.         tempPixmap = QtGui.QPixmap(pixmap)
  79.         child.setPixmap(tempPixmap)      
  80.         if (drag.start(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction):
  81.             child.close()
  82.         else:
  83.             child.show()
  84.             child.setPixmap(pixmap) 
  85.  
  86. # Display a cubic
  87. class CubicDragable(QtGui.QLabel,object):    
  88.     def __init__(self, QP1, QP2, parent=None):
  89.         super (CubicDragable, self).__init__ (parent)
  90.         self.setAcceptDrops(True)
  91.         # Construct the path
  92.         path = QtGui.QPainterPath()
  93.         path.setFillRule(QtCore.Qt.OddEvenFill)
  94.  
  95.         # Drawing the cubic to gap 2 points             
  96.         if (QP1.x()<QP2.x()):
  97.             # Coordinates of the first control point
  98.             QPc1 = QtCore.QPointF((QP2.x()-QP1.x())/2,QP1.y())
  99.             # Coodinates of the second control point
  100.             QPc2 = QtCore.QPointF((QP2.x()-QP1.x())/2,QP2.y())
  101.         else:
  102.             # Coordinates of the first control point
  103.             QPc1 = QtCore.QPointF((QP1.x()-QP2.x())/2,QP1.y())
  104.             # Coodinates of the second control point
  105.             QPc2 = QtCore.QPointF((QP1.x()-QP2.x())/2,QP2.y())
  106.  
  107.         path.moveTo(QP1)
  108.         path.cubicTo(QPc1,QPc2,QP2)
  109.         path.closeSubpath()
  110.  
  111.         # Area where the path will be drawn
  112.         image = QtGui.QImage(abs(QP1.x()- QP2.x()), abs(QP1.y()- QP2.y()),
  113.                              QtGui.QImage.Format_ARGB32_Premultiplied)
  114.  
  115.         # Start the painting device
  116.         painter = QtGui.QPainter()
  117.         painter.begin(image)
  118.         painter.setRenderHint(QtGui.QPainter.Antialiasing)
  119.  
  120.         # Dawing the path of our cubic
  121.         painter.drawPath(path)
  122.         self.setPixmap(QtGui.QPixmap.fromImage(image))
  123.         painter.end()
  124.  
  125.     def dragEnterEvent(self, event):
  126.         if (event.mimeData().hasFormat("application/x-dnditemdata")):
  127.             if (event.source()==self):
  128.                 event.setDropAction(QtCore.Qt.MoveAction)
  129.                 print "on est dans le dragableWidget"
  130.                 event.accept()
  131.             else:
  132.                 event.acceptProposedAction()
  133.         else:
  134.             event.ignore()
  135.  
  136.     def dropEvent(self, event):
  137.         if (event.mimeData().hasFormat("application/x-dnditemdata")):
  138.             itemData = event.mimeData().data("application/x-dnditemdata")
  139.             dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.ReadOnly)
  140.             pixmap = QtGui.QPixmap()
  141.             offset = QtCore.QPoint()           
  142.             dataStream >> pixmap >> offset            
  143.             newIcon = QtGui.QLabel(self)
  144.             newIcon.setPixmap(pixmap)
  145.             newIcon.move(event.pos() - offset)
  146.             newIcon.show()
  147.             newIcon.setAttribute(QtCore.Qt.WA_DeleteOnClose)
  148.  
  149.             if (event.source() == self):
  150.                 event.setDropAction(QtCore.Qt.MoveAction)
  151.                 event.accept()
  152.             else:
  153.                 event.acceptProposedAction()   
  154.         else:
  155.             event.ignore() 
  156.  
  157.     def mousePressEvent(self,event):
  158.         child = self.childAt(event.pos())
  159.         if (not child):
  160.             return 
  161.  
  162.         pixmap = child.pixmap()
  163.         itemData = QtCore.QByteArray()    
  164.         dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.WriteOnly)
  165.         dataStream << pixmap << QtCore.QPoint(event.pos() - child.pos())
  166.         mimeData = QtCore.QMimeData()
  167.         mimeData.setData("application/x-dnditemdata", itemData)
  168.         print "entrer dans le cubic dragable"
  169.         drag = QtGui.QDrag(self)
  170.         drag.setMimeData(mimeData)
  171.         drag.setPixmap(pixmap)
  172.         drag.setHotSpot(event.pos() - child.pos())
  173.         tempPixmap = QtGui.QPixmap(pixmap)        
  174.         child.setPixmap(tempPixmap)
  175.  
  176.         if (drag.start(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction):
  177.             child.close()
  178.         else:
  179.             child.show()
  180.             child.setPixmap(pixmap)  
  181.  
  182.  
  183. # Frame wich allow to drag and drop objects contained in the frame
  184. class DragWidget(QtGui.QFrame,object):
  185.  
  186.     def __init__(self, parent=None):
  187.         QtGui.QFrame.__init__(self, parent)
  188.         self.setMinimumSize(200, 200)
  189.         self.setFrameStyle(QtGui.QFrame.Sunken | QtGui.QFrame.StyledPanel)
  190.         self.setAcceptDrops(True)
  191.         self.setWindowTitle(self.tr("Objets Dragables"))
  192.  
  193.         # ----------------------------
  194.         # Adding of graphicals objects
  195.         # ----------------------------
  196.  
  197.         cd = CubicDragable(QtCore.QPointF(0,0),QtCore.QPointF(100,75),self)
  198.         cd.move(10,10)
  199.         cd.show()
  200.  
  201.         sb = SquareBox(QtCore.QPointF(100,75),self)
  202.         sb.show()
  203.         sb.setAcceptDrops(True)
  204.         help(sb)
  205.  
  206. if __name__ == "__main__":
  207.  
  208.     app = QtGui.QApplication(sys.argv)
  209.     window = DragWidget()
  210.     window.show()
  211.     sys.exit(app.exec_())
May 14 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.