In the end, I made a subclass of PlotWidget :
class PltWidget(pg.PlotWidget):
def __init__(self, parent=None):
super(PltWidget, self).__init__(parent)
self.selectionMode = False
def mousePressEvent(self, ev):
if self.selectionMode:
if ev.button() == QtCore.Qt.LeftButton:
# How do I get the X axis ?
else:
super(PltWidget, self).mousePressEvent(ev)Then I use it in my window, connecting the button signal with the slot changing the boolean of my PltWidget :
..... # Other attributes and connections of my Window
self.T0Button = QtGui.QPushButton()
self.graphicsLeft = PltWidget()
self.T0Button.clicked.connect(self.selectT0)
def selectT0(self):
self.graphicsLeft.selectionMode = not(self.graphicsLeft.selectionMode)However, I still need to know how do I get the X axis of the PlotWidget from where I clicked. If anyone using pyqtgraph know the answer, please let me know. Thanks.
However, I still need to know how do I get the X axis of the PlotWidget from where I clicked. If anyone using pyqtgraph know the answer, please let me know. Thanks.
class CustomViewBox(pg.ViewBox):
def mousePressEvent(self, ev):
if ev.button() == QtCore.Qt.LeftButton:
print('x = {}'.format(self.mapToView(ev.pos()).x()))
else:
super(CustomViewBox, self).mousePressEvent(ev)
p = pg.PlotWidget(viewBox=CustomViewBox())Yeah i knew for the ev.pos().x() but it wasn't the result I wanted since I needed the X axis on the plot. I finally discovered the mapToView methods but didn't really understood in the first place.
Anyway, I still managed to do this and your answer made things more clear so thanks a lot.
Just on a side note, it seems like I have a problem in the way a inherit from PlotWidget.
class PltWidget(pg.PlotWidget):"""Subclass of PlotWidget"""def __init__(self, parent=None):"""Constructor of the widget"""super(PltWidget, self).__init__(parent)self.selectionMode = False # Selection mode used to mark histo dataself.viewBox = self.plotItem.getViewBox() # Get the ViewBox of the widgetself.viewBox.setMouseMode(self.viewBox.RectMode) # Set mouse mode to rect for convenient zoomingself.line1 = pg.InfiniteLine(angle=90, movable=False)self.line2 = pg.InfiniteLine(angle=90, movable=False)self.line1IsDefine = Falseself.line2IsDefine = Falseself.buffer = Nonedef mousePressEvent(self, ev):"""Override the method for selection mode"""
[snip]
With this code, it works exactly the way I want but it seems like there is an issue with the mouseReleaseEvent : When I'm in selectionMode, if I left-click, it does the good job showing the line and printing the X-axis. However it then adds this error in the console :Traceback (most recent call last):File "C:\Anaconda\lib\site-packages\pyqtgraph\GraphicsScene\GraphicsScene.py", line 212, in mouseReleaseEventif self.sendClickEvent(cev[0]):IndexError: list index out of range
Ok, thanks for clearing this up. So I think the "mapToView" that I use currently does the same thing as "mapDeviceToView" ? (because it works exactly the way I expected)
In fact, I already tried to override them but the issue was still there =/ Maybe I was doing it wrong but anyway the other solution works perfectly fine.