I forgot to add the right code.

I have attached the drag and drop events of the graphicsview and the list. 
Executing this code I don't get any errors, but nothing is shown on screen. I 
hope someone can explain what I'm doing wrong.

Mads



________________________________
From: Mads <[email protected]>
To: [email protected]
Sent: Tuesday, June 16, 2009 2:09:00 PM
Subject: [PyQt] Dragging a QGraphicsItem from a QListView


Hi list

I have a mainwindow with a dockwidget which holds a QListView displaying a list 
of different unique names. Also in the window I have a QGraphicsview. I now 
want to be able to drag one of the names displayed in the list onto the view, 
and then be displayed as a QGraphicsItem, which is designed as a rectangle. So 
the question is how do I get the graphicsview to accept the graphicsitem and 
then display it?

I have implemented this dropevent in the graphicsview:

  def dropEvent(self, event):
    node = item.Item(event.pos(), self.scene)
    node.show()
    event.accept()

it is being called and calls the graphicsitem, but it is not shown on screen.

What am I doing wrong?

Mads


      
class View(QGraphicsView):
  def __init__(self, parent=None):
    super(View, self).__init__(parent)
    self.setAcceptDrops(True)

    self.setDragMode(QGraphicsView.RubberBandDrag)
    self.setRenderHint(QPainter.Antialiasing)
    self.setRenderHint(QPainter.TextAntialiasing)

  def wheelEvent(self, event):
    factor = 1.41 ** (-event.delta() / 240.0)
    self.scale(factor, factor)

  def dragMoveEvent(self, event):
    if event.mimeData().hasFormat("application/x-graphicsItem"):
      event.setDropAction(Qt.MoveAction)
      event.accept()
    else:
      event.ignore()

  def dragEnterEvent(self, event):
    event.accept()

  def dropEvent(self, event):
    if event.mimeData().hasFormat("application/x-graphicsItem"):
      data = event.mimeData().data("application/x-graphicsItem")
      self.item = item.Item(event.pos(), self.scene)
      event.setDropAction(Qt.CopyAction)
      event.accept()
      self.updateGeometry()
      self.update()
    else:
      event.ignore()
PointSize = 10

class Item(QGraphicsItem):
  def __init__(self, scene, rect = None, style = Qt.SolidLine):
    super(Item, self).__init__()
    self.setFlags(QGraphicsItem.ItemIsSelectable|
                  QGraphicsItem.ItemIsMovable|
                  QGraphicsItem.ItemIsFocusable)
                  
    if rect is None:
        rect = QRectF(-1 * PointSize, -PointSize,
                      30 * PointSize, 8 * PointSize)

    self.rect = rect
    self.style = style
    self.setSelected(True)
    self.setFocus()
    self.setToolTip("Dragged Item")

  def setStyle(self, style):
    self.style = style
    self.update()

  def paint(self, painter, option, widget):
    pen = QPen(self.style)
    pen.setColor(Qt.black)
    pen.setWidth(1)
    if option.state & QStyle.State_Selected:
        pen.setColor(Qt.blue)
    painter.setPen(pen)
    painter.drawRect(self.rect)

  def boundingRect(self):
    return self.rect.adjusted(-2, -2, 2, 2)
  def dragEnterEvent(self, event):
    if event.mimeData().hasFormat("application/x-graphicsItem"):
      event.accept()
    else:
      event.ignore()

  def dragMoveEvent(self, event):
    if event.mimeData().hasFormat("application/x-graphicsItem"):
      action = Qt.MoveAction
      if event.keyboardModifiers() & Qt.ControlModifier:
        action = Qt.CopyAction
      event.setDropAction(action)
      event.accept()
    else:
      event.ignore()

  def dropEvent(self, event):
    if event.mimeData().hasFormat("application/x-graphicsItem"):
      data = event.mimeData().data("application/x-graphicsItem")
      node = item.Item(event.pos(), self.scene)
      action = Qt.MoveAction
      if event.keyboardModifiers() & Qt.ControlModifier:
        action = Qt.CopyAction
      event.setDropAction(action)
      event.accept()
    else:
      event.ignore()

  def startDrag(self, dropActions):
    item = self.currentItem()
    data = QByteArray()
    mimeData = QMimeData()
    mimeData.setData("application/x-graphicsItem", data)
    drag = QDrag(self)
    drag.setMimeData(mimeData)
    if drag.start(Qt.MoveAction|Qt.CopyAction) == Qt.MoveAction:
      self.takeItem(self.row(item))
_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to