viewbox and legend item

37 views
Skip to first unread message

clément Andre

unread,
Feb 16, 2020, 10:39:07 AM2/16/20
to pyqtgraph
Hello,
I am looking for create an IHM able to plotting many curves on each others (so far no problem)
in order to give good/clear informations to users, i would like to add legend/items in a viewbox placed on the right of the plot (here again, no problem).

I am blocked concerning the position setting of the item in the viewbox...
Indeed, I would like to fix the position of the legend in the viewbox

here is two ways to create legend/items... but both present the an issue:
self.column_legend = self.graph.addViewBox(border=QPen(QColor(255, 0, 0), 1, Qt.SolidLine), enableMouse=False)
self.column_legend.setMaximumWidth(80)
self.legend = pg.LegendItem(size=(10, 20), offset=(1,1))
self.legend.setParentItem(self.colonne_legende)
self.column_legend.setMouseEnabled(x=False, y=False)
here, i created wiewbox, legenditem... but despite the setting of enableMouse to False, items are movable!

self.column_legend = self.graph.addViewBox(border=QPen(QColor(255, 0, 0), 1, Qt.SolidLine), enableMouse=False)
self.column_legend.setMaximumWidth(80)
self.legend = pg.LegendItem(size=(10, 20), offset=None)
self.column_legend.addItem(self.legend)
self.column_legend.setMouseEnabled(x=False, y=False)
here, items are not movable!!! but offset need to set "None"... so the position is bad...

is there a solution to do what i am looking for?
thanks

Patrick

unread,
Feb 17, 2020, 7:01:10 AM2/17/20
to pyqtgraph
Hi,

I don't totally understand all of the pyqtgraph GraphicsScene modifications, but you're right that the usual things you'd do for a QGraphicsScene don't seem to work.

Here's a crude way to disable mouse interactions, by live patching the legend mouse handler methods. I'm 99% sure this isn't the best way to do it, but it works.

# Crude patch of the legend mouse methods
import types
def nullifyEvent(self, ev):
   
return None
self.legend.hoverEvent = types.MethodType(nullifyEvent, self.legend)
self.legend.mouseDragEvent = types.MethodType(nullifyEvent, self.legend)


Patrick
Reply all
Reply to author
Forward
0 new messages