[PySide] QGraphicsView and QGraphicsScene
Lucas Tanure
ltanure at gmail.com
Mon Feb 20 22:42:41 CET 2012
Hi,
I'm trying to develop an Drag And Drop with QGraphicsView and
QGraphicsScene. An image comes from another widget and I drop at the scene.
When I add this item I set (at dropEvent)
item.setFlags(QtGui.QGraphicsItem.ItemIsSelectable |
QtGui.QGraphicsItem.ItemIsMovable)
self.addItem(block)
Well, this works, and I can move this item on the scene. But I have two
problems. First is that the first item always goes to position (0,0), but
the second one I can add at any position. Just the first that anywhere I
drop goes to (0,0).
This is because I don't set the size of the scene, and the first one grows
the scene, so for the second works. But here comes my second problem, if I
set some size to this scene I lose the auto scrollbar from the view
(QGraphicsView).
So If drop somewhere on the scene near of the edge or out of the view, I
can't go to this position , because there isn't scrollbars. But I can drop
at anywhere, and the item stays there.
Please Help.
*CODE*
*Main Controll*
self.scene = dragGraphicsScene()
self.ui.blocksGraphicsView.setScene(self.scene)
class dragGraphicsScene(QtGui.QGraphicsScene):
def __init__(self, parent=None):
super(dragGraphicsScene,self).__init__(parent)
def dragEnterEvent(self, event):
if event.mimeData().hasFormat('image/x-block'):
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasFormat('image/x-block'):
#event.setDropAction(QtCore.Qt.MoveAction)
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasFormat('image/x-block'):
itemData = event.mimeData().data('image/x-block')
dataStream = QtCore.QDataStream(itemData,
QtCore.QIODevice.ReadOnly)
pixmap = QtGui.QPixmap()
offset = QtCore.QPoint()
dataStream >> pixmap >> offset
block = QtGui.QGraphicsPixmapItem(pixmap)
block.setPos(event.scenePos()-offset)
block.setFlags(QtGui.QGraphicsItem.ItemIsSelectable |
QtGui.QGraphicsItem.ItemIsMovable)
self.addItem(block)
if event.source() == self:
event.setDropAction(QtCore.Qt.MoveAction)
event.accept()
else:
event.acceptProposedAction()
else:
event.ignore()
*Widget where the drag starts:*
class dragWidget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasFormat('image/x-block'):
event.accept()
else:
event.ignore()
def mousePressEvent(self, event):
child = self.childAt(event.pos())
pixmap = QtGui.QPixmap(child.pixmap())
itemData = QtCore.QByteArray()
dataStream = QtCore.QDataStream(itemData,
QtCore.QIODevice.WriteOnly)
dataStream << pixmap << QtCore.QPoint(event.pos() - child.pos())
mimeData = QtCore.QMimeData()
mimeData.setData("image/x-block", itemData)
#mimeData.setImageData(pixmap)
drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
drag.setPixmap(pixmap)
drag.setHotSpot(event.pos() - child.pos())
#use exec_ instead of start.
drag.exec_(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction)
Lucas A. Tanure Alves
Skype : lucas.tanure
+55 (19) 88176559
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20120220/2e8b07f6/attachment.html>
More information about the PySide
mailing list