Перегрузка PlotWidget
и BarGraphItem
.Проверьте, находится ли позиция QMouseEvent
вашего PlotWidget.mousePressEvent
внутри одного из ваших баров:
import pyqtgraph as pg
class MyBarGraphItem(pg.BarGraphItem):
def __init__(self):
super().__init__()
def setAttr(self, **opts):
if 'x' in opts:
self.x = opts['x']
if 'height' in opts:
self.height = opts['height']
if 'width' in opts:
self.width = opts['width']
if 'brushes' in opts:
self.brushes = opts['brushes']
super().setOpts(**opts)
class MyPlot(pg.PlotWidget):
def __init__(self):
super().__init__()
self.bars = None
def mousePressEvent(self, ev):
pos = self.getPlotItem().vb.mapSceneToView(ev.pos())
if self.bars is not None:
for i,_ in enumerate(self.bars.x):
if self.bars.x[i]-self.bars.width/2 < pos.x() < self.bars.x[i]+self.bars.width/2\
and 0 < pos.y() < self.bars.height[i]:
b = self.bars.brushes
b[i] = pg.QtGui.QColor(255,255,255)
self.bars.setAttr(brushes=b)
print('clicked on bar '+str(i))
ev.accept()
super().mousePressEvent(ev)
def addBars(self, bars):
self.bars = bars
self.addItem(bars)
if __name__ == '__main__':
app = pg.mkQApp()
plot = MyPlot()
bars = MyBarGraphItem()
bars.setAttr(brushes=[pg.hsvColor(float(x) / 5) for x in range(5)], x=[i for i in range(5)], height=[1,5,2,4,3], width=0.5)
plot.addBars(bars)
plot.show()
app.exec()