I have Qt application where I draw graphs using pyqtgraph lib and use dock containers also from pyqtgraph. So I create several dock containers and put one graph in each. Then, in my code I need to identify the graph with which user is working to update some other staff in the application (e.g. to show some properties for "active" graph in some common for all widget).
My idea is to receive mouse click signal and identify the graph where it was clicked to know the last graph user interacted. But pyqtgraph PlotWidget, PlotItem or ViewBox classes don't provide such a signals and I don't know if there is a way to implement it myself. Also, I didn't find a way to identify which dock container is active. I only see sigMouseReleased for the PlotWidget but even this doesn't work for me (see code below)
Here is my minimum code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
from pyqtgraph.dockarea import *
# I use Qt Designer, below I just cut generated code to minimum
class Ui_StartForm(object):
def setupUi(self, StartForm):
StartForm.setObjectName("StartForm")
StartForm.resize(1507, 968)
self.GraphLayout = QtWidgets.QGridLayout(StartForm)
# my application
class AppWindow(QtWidgets.QWidget, Ui_StartForm):
def __init__(self):
super(AppWindow, self).__init__()
self.setupUi(self)
self.dock_area_main = DockArea()
self.GraphLayout.addWidget(self.dock_area_main)
self.Dock1 = Dock("Dock 1", size=(1, 1))
self.dock_area_main.addDock(self.Dock1, 'left')
self.Dock2 = Dock("Dock 2", size=(1, 1))
self.dock_area_main.addDock(self.Dock2, 'right')
self.GraphViewList = []
self.pl1 = pg.PlotWidget()
self.pl2 = pg.PlotWidget()
self.Dock1.addWidget(self.pl1)
self.Dock2.addWidget(self.pl2)
self.pl1.sigMouseReleased.connect(self.mouse_release) # try to get some mouse event
def mouse_release(self):
print('click') # never execute
app = QtWidgets.QApplication(sys.argv)
w = AppWindow()
w.show()
sys.exit(app.exec_())
My question is how can I implement signal mouse clicked for pyqtgraph PlotItem or ViewBox to identify which graph was last interacted by user. Same time, it shouldn't influence functions of pyqtgraph plots (it should catch all mouse events normally)
If there is better strategy to do so - please suggest
PyQtGraph does implement a sigMouseClicked
signal in the GraphicsScene
class, but somehow this is undocumented. The GraphicsScene page only explains about why it implements a parallel mouse event system, but if you look at the source you see that it also emits some handy signals.
Since these are undocumented you should use them at your own risk! Perhaps they will change in the future, although I think that's unlikely. Or maybe you can open an issue and ask for them to be officially supported.
The signal has the originating mouse event as a parameter. There is no reference to the plot that was clicked, but if you can resolve this by overriding the pg.PlotWidget
and connecting to a slot of that derived class. Like so...
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
from pyqtgraph.dockarea import *
# I use Qt Designer, below I just cut generated code to minimum
class Ui_StartForm(object):
def setupUi(self, StartForm):
StartForm.setObjectName("StartForm")
StartForm.resize(1507, 968)
self.GraphLayout = QtWidgets.QGridLayout(StartForm)
class MyPlotWidget(pg.PlotWidget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# self.scene() is a pyqtgraph.GraphicsScene.GraphicsScene.GraphicsScene
self.scene().sigMouseClicked.connect(self.mouse_clicked)
def mouse_clicked(self, mouseClickEvent):
# mouseClickEvent is a pyqtgraph.GraphicsScene.mouseEvents.MouseClickEvent
print('clicked plot 0x{:x}, event: {}'.format(id(self), mouseClickEvent))
# my application
class AppWindow(QtWidgets.QWidget, Ui_StartForm):
def __init__(self):
super(AppWindow, self).__init__()
self.setupUi(self)
self.dock_area_main = DockArea()
self.GraphLayout.addWidget(self.dock_area_main)
# Best to use lower case for variables and upper case for types, so I
# renamed self.Dock1 to self.dock1.
self.dock1 = Dock("Dock 1", size=(1, 1))
self.dock_area_main.addDock(self.dock1, 'left')
self.dock2 = Dock("Dock 2", size=(1, 1))
self.dock_area_main.addDock(self.dock2, 'right')
self.GraphViewList = []
self.pl1 = MyPlotWidget()
self.pl2 = MyPlotWidget()
self.dock1.addWidget(self.pl1)
self.dock2.addWidget(self.pl2)
app = QtWidgets.QApplication(sys.argv)
w = AppWindow()
w.show()
sys.exit(app.exec_())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With