Reputation: 91
I prepared correct onMouse moved function and it shows correctly coordinates of my mouse, but when I clicked somewhere, python prints coordinates of my window not of my plot. I tried to make it the same like for the onMouseMoved but it doesn't work. I also tried to use mouseClickEvent.scenePos() instead od mouseClickEvent but it also doesn't work.
import sys
from PyQt5 import QtWidgets
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.graphWidget = pg.PlotWidget()
self.setCentralWidget(self.graphWidget)
hour = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
temperature = [30, 32, 34, 32, 33, 31, 29, 32, 35, 45]
self.graphWidget.plot(hour, temperature)
self.label = pg.TextItem(text="X: {} \nY: {}".format(0, 0))
self.graphWidget.addItem(self.label)
self.setMouseTracking(True)
self.graphWidget.scene().sigMouseMoved.connect(self.onMouseMoved)
self.graphWidget.scene().sigMouseClicked.connect(self.mouse_clicked)
def onMouseMoved(self, evt):
if self.graphWidget.plotItem.vb.mapSceneToView(evt):
point = self.graphWidget.plotItem.vb.mapSceneToView(evt)
self.label.setHtml(
"<p style='color:white'>X: {0} <br> Y: {1}</p>". \
format(point.x(), point.y()))
def mouse_clicked(self, mouseClickEvent):
# mouseClickEvent is a pyqtgraph.GraphicsScene.mouseEvents.MouseClickEvent
print('clicked plot 0x{:x}, event: {}'.format(id(self), mouseClickEvent))
def main():
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Upvotes: 1
Views: 2540
Reputation: 2550
Asked question is very legitimate.
Indeed evt object from MouseMoused and MouseClicked is not the same.
For MouseMoved You got QPoint
and for MouseClicked MouseClickEvent
.
Reason for that is simple.
How would You know which mouse button was clicked, if You've got only x, y position?
So, to make things works "the same" we need to convert our MouseClickEvent into QPoint.
This can be done with evt.ScenePos()
Here is Your modified code:
import sys
import pyqtgraph as pg
from PyQt5 import QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.graphWidget = pg.PlotWidget()
self.setCentralWidget(self.graphWidget)
hour = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
temperature = [30, 32, 34, 32, 33, 31, 29, 32, 35, 45]
self.graphWidget.plot(hour, temperature)
self.label = pg.TextItem(text="X: {} \nY: {}".format(0, 0))
self.graphWidget.addItem(self.label)
self.graphWidget.scene().sigMouseMoved.connect(self.mouse_moved)
self.graphWidget.scene().sigMouseClicked.connect(self.mouse_clicked)
def mouse_moved(self, evt):
vb = self.graphWidget.plotItem.vb
if self.graphWidget.sceneBoundingRect().contains(evt):
mouse_point = vb.mapSceneToView(evt)
self.label.setHtml(f"<p style='color:white'>X: {mouse_point.x()} <br> Y: {mouse_point.y()}</p>")
def mouse_clicked(self, evt):
vb = self.graphWidget.plotItem.vb
scene_coords = evt.scenePos()
if self.graphWidget.sceneBoundingRect().contains(scene_coords):
mouse_point = vb.mapSceneToView(scene_coords)
print(f'clicked plot X: {mouse_point.x()}, Y: {mouse_point.y()}, event: {evt}')
def main():
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Upvotes: 2