[PyQt] PyQt4: QGraphicsView/drag&drop problem

Eriol eriol at mornie.org
Thu Jun 21 21:33:45 BST 2007


On Thursday 21 June 2007, Frank Ploss wrote:
> I'm trying to implement drag and drop with a QGraphicsView.

Can't you use the default Graphics View framework drag and drop support 
provided for the scene, and for each item?

----8<----------------------------------

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

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

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

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_()

------>8----------------------------------------

If not, consider that (from the doc: 
http://doc.trolltech.com/4.2/graphicsview.html#drag-and-drop):

To intercept drag and drop events for the scene, you reimplement 
QGraphicsScene::dragEnterEvent() and whichever event handlers your 
particular scene needs, in a QGraphicsItem subclass.

Items can enable drag and drop support by calling 
QGraphicsItem::setAcceptDrops(). To handle the incoming drag, reimplement 
QGraphicsItem::dragEnterEvent(), QGraphicsItem::dragMoveEvent(), 
QGraphicsItem::dragLeaveEvent(), and QGraphicsItem::dropEvent().

HTH,

-- 
 Eriol - *p = NULL; - EIBTI
 GPG Key ID 0B7C8A19
 http://mornie.org


More information about the PyQt mailing list