سؤال

  1. I want to drag an item from a QListWidget into pyqtgraph.plotItem. How can I do that.

  2. When I click on a plotItem I want to know which item has been clicked. I think I need to reimplement some classes' method. Then which class? Many thanks.

هل كانت مفيدة؟

المحلول

There are a few things you need:

  1. Override pg.GraphicsView.dragEnterEvent() to accept the event.
  2. Call pg.PlotItem.setAcceptDrops(True)
  3. Override pg.PlotItem.dropEvent() to handle the drop

You can override methods either by creating a subclass or by simply re-assigning the method on an existing object. Example:

import pyqtgraph as pg
app = pg.QtGui.QApplication([])

l = pg.QtGui.QListWidget()
l.addItem('Drag me')
l.setDragDropMode(l.DragOnly)
l.show()

win = pg.GraphicsWindow()
win.show()

def dragEnterEvent(ev):
    ev.accept()

win.dragEnterEvent = dragEnterEvent

plot = pg.PlotItem()
plot.setAcceptDrops(True)
win.addItem(plot)

def dropEvent(event):
    print "Got drop!"

plot.dropEvent = dropEvent
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top