QGraphicsItem and dragEvents



  • Hey Community,

    I can't get my drag events to work on QGraphicsItem. Even with the setAcceptDrops(True), is there something else to do to enable them ? They work by default on regular widgets...
    Since QGraphicsItems are movable, I understand that it might be a little bit different but can't figure this out.

    here is a sample code :

    from PySide import QtGui, QtCore
    
    class GraphicItem(QtGui.QGraphicsItem):
    
        def __init__(self, parent=None):
            super(GraphicItem, self).__init__(parent)
    
            self.setAcceptDrops(True)
    
        def boundingRect(self):
            rect = QtCore.QRect(0,0,400,400)
            return rect
    
        def shape(self):
            path = QtGui.QPainterPath()
            path.addRect(self.boundingRect())
            return path
    
        def paint(self, painter, option, widget):
            painter.setBrush(QtGui.QColor(255,0,0,255))
            painter.drawRect(self.boundingRect())
    
        def dragEnterEvent(self, event):
            print 'drag enter'
            super(GraphicItem, self).dragEnterEvent(event)
    
        def dragMoveEvent(self, event):
            print 'drag move'
            super(GraphicItem, self).dragMoveEvent(event)
    
    
    s = QtGui.QGraphicsScene()
    v = QtGui.QGraphicsView(s)
    v.show()
    
    ti = GraphicItem(parent=None)
    s.addItem(ti)
    

    Thanks


  • Lifetime Qt Champion

    Hi,

    Do you want to move your items around ?



  • Nope, It is just a static item that it driven by a parent.
    For now I have to do it with mouse press/move/release events + a condition ...
    It works but i feel like the drag events are designed for that.

    Or, am I wrong ?


  • Lifetime Qt Champion

    Then what exactly do you want to achieve using drag and drop ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.