Emit signal when GraphicScene.addItem() is used (#1388)

* Emit signal when GraphicScene.addItem() is used

Emit a signal (sigItemAdded) just after adding an item to a scene.
The item object is emited as the only signal argument.
This signal is useful for code that may want to react to newly added
items of a plot.

* Emit signal when GraphicScene.removeItem() is used

Emit a signal (sigItemRemoved) just after removing an item from a scene.
The item object is emited as the only signal argument.
This signal is useful for code that may want to react to removed
items in a plot.
This commit is contained in:
Carlos Pascual 2020-11-06 23:06:53 +01:00 committed by GitHub
parent 98c01a3667
commit 3aa3e9b991
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 3 deletions

View File

@ -46,7 +46,7 @@ class GraphicsScene(QtGui.QGraphicsScene):
* Eats mouseMove events that occur too soon after a mouse press.
* Reimplements items() and itemAt() to circumvent PyQt bug
====================== ==================================================================
====================== ====================================================================
**Signals**
sigMouseClicked(event) Emitted when the mouse is clicked over the scene. Use ev.pos() to
get the click position relative to the item that was clicked on,
@ -56,7 +56,9 @@ class GraphicsScene(QtGui.QGraphicsScene):
is given in scene coordinates.
sigMouseHover(items) Emitted when the mouse is moved over the scene. Items is a list
of items under the cursor.
====================== ==================================================================
sigItemAdded(item) Emitted when an item is added via addItem(). The item is given.
sigItemRemoved(item) Emitted when an item is removed via removeItem(). The item is given.
====================== ====================================================================
Mouse interaction is as follows:
@ -90,7 +92,10 @@ class GraphicsScene(QtGui.QGraphicsScene):
sigMouseClicked = QtCore.Signal(object) ## emitted when mouse is clicked. Check for event.isAccepted() to see whether the event has already been acted on.
sigPrepareForPaint = QtCore.Signal() ## emitted immediately before the scene is about to be rendered
sigItemAdded = QtCore.Signal(object) ## emits the item object just added
sigItemRemoved = QtCore.Signal(object) ## emits the item object just removed
_addressCache = weakref.WeakValueDictionary()
ExportDirectory = None
@ -393,6 +398,18 @@ class GraphicsScene(QtGui.QGraphicsScene):
break
self.sigMouseClicked.emit(ev)
return ev.isAccepted()
def addItem(self, item):
# extend QGraphicsScene.addItem to emit a sigItemAdded signal
ret = QtGui.QGraphicsScene.addItem(self, item)
self.sigItemAdded.emit(item)
return ret
def removeItem(self, item):
# extend QGraphicsScene.removeItem to emit a sigItemRemoved signal
ret = QtGui.QGraphicsScene.removeItem(self, item)
self.sigItemRemoved.emit(item)
return ret
def items(self, *args):
items = QtGui.QGraphicsScene.items(self, *args)