How to get notified when a new item is added to a Plot?

Visto 20 veces
Saltar al primer mensaje no leído

Carlos Pascual

no leída,
29 nov 2017, 3:16:2329/11/17
a pyqtgraph
Hi, 

I've been looking for a way of getting a signal when a new item is added to a PlotWidget or ViewBox

Ideally something in the style of "ViewBox.sigItemAdded"

I understand that I could derive ViewBox and/or PlotWidget and reimplement the addItem() method to emit such a signal, but this is really something I would like to avoid because I would need my code to work with the "unmodified" PyQtgraph objects.

So I am wondering if this is already possible in some other way (or if it would be a wanted feature, in which case I would be happy to submit a Pull Request with an implementation)

Sebastian Höfer

no leída,
29 nov 2017, 6:16:4029/11/17
a pyqtgraph
Hi,

if you don't want to create your own class a quick and dirty way is to "monkey patch" the addItem() method and insert your own code there.
Something along the lines:

oldAddItem = the_viewbox.addItem

def myAddFunc(*args, **kwargs):
   
# do something before the item is added
    oldAddItem
(*args, **kwargs)
   
# do something after the item is added
   
# or yourSignal.emit()

the_viewbox
.addItem = myAddFunc

I haven't tested the code, so this might need tweaking.


Cheers,
Sebastian

Carlos Pascual

no leída,
29 nov 2017, 7:34:0729/11/17
a pyqt...@googlegroups.com,Sebastian Höfer
Hi Sebastian,

Thanks for the quick answer!
Unfortunately monkey-patching also disqualifies from my requirement of using
unmodified pyqtgraph objects.
I was hoping that I had missed some existing mechanism (a signal, or a QEvent)
in some base class that could be used to be notified of new items being added.

If that is not the case, the second part of my question is relevant: would a
PR adding a signal be welcome?

Cheers!

Carlos
--
+----------------------------------------------------+
Carlos Pascual Izarra
Scientific Software Coordinator
Computing Division
ALBA Synchrotron [http://www.albasynchrotron.es]
Carrer de la Llum 2-26
E-08290 Cerdanyola del Valles (Barcelona), Spain
E-mail: cpas...@cells.es
Phone: +34 93 592 4428
+----------------------------------------------------+
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos