Affichage d'une cubiquer Python/Qt4

Affichage d'une cubiquer Python/Qt4 - Python - Programmation

Marsh Posté le 23-04-2007 à 10:52:15    

Bonjour à tous,
 
J'éssaye d'afficher une cubique dans un QLabel contenu dans l'objet "CubicDragable". La fenêtre se lance mais rien de s'affiche.
 
Auriez vous une idée? :
 

Code :
  1. # Display a cubic
  2. class CubicDragable(QtGui.QLabel):
  3.     def __init__(self, parent=None):
  4.         super (CubicDragable, self).__init__ (parent)
  5.         # Construct the path
  6.         path = QtGui.QPainterPath()
  7.         path.cubicTo(QtCore.QPointF(0,0),QtCore.QPointF(50,50),QtCore.QPointF(0,25))
  8.         path.moveTo(-0.083695, 0.283849)
  9.         path.cubicTo(-0.049581, 0.349613, -0.012720, 0.397969, 0.026886, 0.428917)
  10.         path.cubicTo(0.066493, 0.459865, 0.111593, 0.477595, 0.162186, 0.482108)
  11.         path.lineTo(0.162186, 0.500000)
  12.         path.cubicTo(0.115929, 0.498066, 0.066565, 0.487669, 0.014094, 0.468810)       
  13.         path.cubicTo(-0.038378, 0.449952, -0.088103, 0.423839, -0.135082, 0.390474)
  14.         path.cubicTo(-0.182061, 0.357108, -0.222608, 0.321567, -0.256722, 0.283849)
  15.         path.cubicTo(-0.304712, 0.262250, -0.342874, 0.239362, -0.371206, 0.215184)
  16.         path.cubicTo(-0.411969, 0.179078, -0.443625, 0.134671, -0.466175, 0.081963)
  17.         path.cubicTo(-0.488725, 0.029255, -0.500000, -0.033043, -0.500000, -0.104932)
  18.         path.cubicTo(-0.500000, -0.218407, -0.467042, -0.312621, -0.401127, -0.387573)
  19.         path.cubicTo(-0.335212, -0.462524, -0.255421, -0.500000, -0.161752, -0.500000)
  20.         path.cubicTo(-0.072998, -0.500000, 0.003903, -0.462444, 0.068951, -0.387331)
  21.         path.cubicTo(0.133998, -0.312218, 0.166522, -0.217440, 0.166522, -0.102998)
  22.         path.cubicTo(0.166522, -0.010155, 0.143394, 0.071325, 0.097138, 0.141441)
  23.         path.cubicTo(0.050882, 0.211557, -0.009396, 0.259026, -0.083695, 0.283849)
  24.         path.moveTo(-0.167823, -0.456963)
  25.         path.cubicTo(-0.228823, -0.456963, -0.277826, -0.432624, -0.314831, -0.383946)
  26.         path.cubicTo(-0.361665, -0.323340, -0.385082, -0.230335, -0.385082, -0.104932)
  27.         path.cubicTo(-0.385082, 0.017569, -0.361376, 0.112025, -0.313964, 0.178433)
  28.         path.cubicTo(-0.277248, 0.229368, -0.228534, 0.254836, -0.167823, 0.254836)
  29.         path.cubicTo(-0.105088, 0.254836, -0.054496, 0.229368, -0.016045, 0.178433)
  30.         path.cubicTo(0.029055, 0.117827, 0.051605, 0.028691, 0.051605, -0.088975)
  31.         path.cubicTo(0.051605, -0.179562, 0.039318, -0.255803, 0.014744, -0.317698)
  32.         path.cubicTo(-0.004337, -0.365409, -0.029705, -0.400548, -0.061362, -0.423114)
  33.         path.cubicTo(-0.093018, -0.445680, -0.128505, -0.456963, -0.167823, -0.456963)
  34.         path.moveTo(0.379011, -0.404739)
  35.         path.lineTo(0.379011, -0.236460)
  36.         path.lineTo(0.486123, -0.236460)
  37.         path.lineTo(0.486123, -0.197292)
  38.         path.lineTo(0.379011, -0.197292)
  39.         path.lineTo(0.379011, 0.134913)
  40.         path.cubicTo(0.379011, 0.168117, 0.383276, 0.190442, 0.391804, 0.201886)
  41.         path.cubicTo(0.400332, 0.213330, 0.411246, 0.219052, 0.424545, 0.219052)
  42.         path.cubicTo(0.435531, 0.219052, 0.446227, 0.215264, 0.456635, 0.207689)
  43.         path.cubicTo(0.467042, 0.200113, 0.474993, 0.188910, 0.480486, 0.174081)
  44.         path.lineTo(0.500000, 0.174081)
  45.         path.cubicTo(0.488436, 0.210509, 0.471957, 0.237911, 0.450564, 0.256286)
  46.         path.cubicTo(0.429170, 0.274662, 0.407054, 0.283849, 0.384215, 0.283849)
  47.         path.cubicTo(0.368893, 0.283849, 0.353859, 0.279094, 0.339115, 0.269584)
  48.         path.cubicTo(0.324371, 0.260074, 0.313530, 0.246534, 0.306592, 0.228965)
  49.         path.cubicTo(0.299653, 0.211396, 0.296184, 0.184075, 0.296184, 0.147002)
  50.         path.lineTo(0.296184, -0.197292)
  51.         path.lineTo(0.223330, -0.197292)
  52.         path.lineTo(0.223330, -0.215667)
  53.         path.cubicTo(0.241833, -0.224049, 0.260697, -0.237992, 0.279922, -0.257495)
  54.         path.cubicTo(0.299147, -0.276999, 0.316276, -0.300129, 0.331310, -0.326886)
  55.         path.cubicTo(0.338826, -0.341070, 0.349523, -0.367021, 0.363400, -0.404739)
  56.         path.lineTo(0.379011, -0.404739)
  57.         path.moveTo(-0.535993, 0.275629)
  58.         path.closeSubpath()
  59.        
  60.         # Color 1
  61.         qCol = QtGui.QColor(60, 60, 60, 63)
  62.         # Area where the path will be drawn
  63.         image = QtGui.QImage(200, 200,
  64.                              QtGui.QImage.Format_ARGB32_Premultiplied)
  65.         # Background color of the Area "image"
  66.         image.fill(QtGui.qRgba(0, 0, 0, 0))
  67.        
  68.         # Start the painting device
  69.         painter = QtGui.QPainter()
  70.         painter.begin(image)
  71.         painter.setBrush(qCol);
  72.         painter.setPen(QtGui.QPen(qCol, 10,
  73.                                   QtCore.Qt.SolidLine, QtCore.Qt.RoundCap,
  74.                                   QtCore.Qt.RoundJoin))
  75.         # Dawing the path of our cubic
  76.         painter.drawPath(path)
  77.         # Drawing a rectangle
  78.         painter.drawRoundRect(0,10,30,10,2,2)
  79.         painter.end()
  80. # Frame wich allow to drag and drop objects contained in the frame
  81. class DragWidget(QtGui.QFrame):
  82.    
  83.     def __init__(self, parent=None):
  84.         QtGui.QFrame.__init__(self, parent)
  85.         self.setMinimumSize(200, 200)
  86.         self.setFrameStyle(QtGui.QFrame.Sunken | QtGui.QFrame.StyledPanel)
  87.         self.setAcceptDrops(True)
  88.         self.setWindowTitle(self.tr("Objets Dragables" ))
  89.         cd = CubicDragable()
  90.         cd.move(10,10)
  91.         cd.show()
  92.        
  93.          
  94.     def dragEnterEvent(self, event):
  95.         if (event.mimeData().hasFormat("application/x-dnditemdata" )):
  96.             if (event.source()==self):
  97.                 event.setDropAction(QtCore.Qt.MoveAction)
  98.                 event.accept()
  99.             else:
  100.                 event.acceptProposedAction()
  101.         else:
  102.             event.ignore()
  103.     def dropEvent(self, event):
  104.         # Getting the properties of the object dragable
  105.         # Position
  106.         pos = QtCore.QPoint(event.pos().x(), event.pos().y())
  107.      
  108.         if (event.mimeData().hasFormat("application/x-dnditemdata" )):
  109.             itemData = event.mimeData().data("application/x-dnditemdata" )
  110.             dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.ReadOnly)
  111.             # pixmap = QtGui.QPixmap(item.data(QtCore.Qt.DecorationRole))
  112.             pixmap = QtGui.QPixmap()
  113.             offset = QtCore.QPoint()
  114.            
  115.             dataStream >> pixmap >> offset
  116.            
  117.             newIcon = QtGui.QLabel(self)
  118.             newIcon.setPixmap(pixmap)
  119.             newIcon.move(event.pos() - offset)
  120.             newIcon.show()
  121.             newIcon.setAttribute(QtCore.Qt.WA_DeleteOnClose)
  122.             if (event.source() == self):
  123.                 event.setDropAction(QtCore.Qt.MoveAction)
  124.                 event.accept()
  125.             else:
  126.                 event.acceptProposedAction()
  127.                
  128.         else:
  129.             event.ignore()   
  130.        
  131.        
  132.     def mousePressEvent(self,event):
  133.         child = self.childAt(event.pos())
  134.         if (not child):
  135.             return
  136.        
  137.         pixmap = child.pixmap()
  138.         itemData = QtCore.QByteArray()   
  139.         dataStream = QtCore.QDataStream(itemData, QtCore.QIODevice.WriteOnly)
  140.         dataStream << pixmap << QtCore.QPoint(event.pos() - child.pos())
  141.        
  142.         mimeData = QtCore.QMimeData()
  143.         mimeData.setData("application/x-dnditemdata", itemData)
  144.        
  145.        
  146.         drag = QtGui.QDrag(self)
  147.         drag.setMimeData(mimeData)
  148.         drag.setPixmap(pixmap)
  149.         drag.setHotSpot(event.pos() - child.pos())
  150.        
  151.         tempPixmap = QtGui.QPixmap(pixmap)
  152.                      
  153.         child.setPixmap(tempPixmap)
  154.        
  155.         if (drag.start(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction):
  156.             child.close()
  157.         else:
  158.             child.show()
  159.             child.setPixmap(pixmap) 
  160.    
  161. if __name__ == "__main__":
  162.     app = QtGui.QApplication(sys.argv)
  163.     window = DragWidget()
  164.     window.show()
  165.     sys.exit(app.exec_())

Reply

Marsh Posté le 23-04-2007 à 10:52:15   

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed