[PyQt] PyQt4: QGraphicsView/drag&drop problem

Frank Ploss frank.ploss at informatik.uni-hamburg.de
Thu Jun 21 00:26:35 BST 2007


Hi,

I'm quite new to Qt programming, so maybe this is easy ...

I'm using PyQt4 4.1 on Ubuntu feisty, Qt version is 4.2.2.

I'm trying to implement drag and drop with a QGraphicsView. With the
following code, it should be possible to drag from the rectangle and drop
into the empty space in the scene. Starting the drag works, but when
hovering over the empty space, the mouse pointer indicates that dropping
isn't possible and dropEvent() is never reached, although I
acceptProposedAction()'ed in dragEnterEvent(). As I understand it, this
should indicate to the drag object that dropping is possible.

What's wrong with this?

Best regards,
Frank

-----------------------------
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class MyItem(QGraphicsRectItem):
   def __init__(self):
      QGraphicsRectItem.__init__(self, QRectF(20, 20, 100, 100))

   def mousePressEvent(self, event):
      print "mousePressEvent: %s (event type: %d)" % (type(event),  \
event.type())
      md = QMimeData()
      md.setText("hello world")
      drag = QDrag(event.widget())
      drag.setMimeData(md)
      drag.start(Qt.MoveAction)

class MyScene(QGraphicsScene):
   def __init__(self):
      QGraphicsScene.__init__(self)
      self.box = MyItem()
      self.addItem(self.box)

   def dragEnterEvent(self, event):
      print "dragEnterEvent: %s (event type: %d)" % (type(event), \
event.type())
      event.acceptProposedAction()

   def dropEvent(self, event):
      print "dropEvent: %s (event type: %d)" % (type(event), event.type())
      event.acceptProposedAction()

qapp = QApplication(sys.argv)
myScene = MyScene()
myScene.setSceneRect(0, 0, 400, 400)
view = QGraphicsView()
view.setMinimumSize(400, 400)
view.setAcceptDrops(True)
view.setScene(myScene)
view.show()

qapp.exec_()
----------------------------------



More information about the PyQt mailing list