Hi,
I've tried to get crosshairs working on a plot within a GraphicsLayoutWidget using the designerExample.py/ui from the github as a basis but have hit a couple of snags.
I constantly get :
Traceback (most recent call last):
File "/home/mark/PycharmProjects/SIDDataAnalyser/mynew.py", line 90, in mouseMoved
pos = evt[0] ## using signal proxy turns original arguments into a tuple
TypeError: 'QPointF' object does not support indexing
evt = (PyQt4.QtCore.QPointF(266.0, 0.0),)
When I select the plotButton within the widget and move the my mouse back over the plot window the plot then shrinks.
Could anyone give me pointers on where I've gone wrong?
I've attached my designExample.ui and py files as well as listing them below.
I'm using Python 3 and pyqtgraph 4.8.6
Any help much appreciated,
Mark
designExample.ui
----------------------------
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QPushButton" name="plotBtn">
<property name="text">
<string>Plot!</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="GraphicsLayoutWidget" name="plot"/>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>GraphicsLayoutWidget</class>
<extends>QGraphicsView</extends>
<header>pyqtgraph</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
=======================================================
designExample.py
---------------------------
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
import os
pg.mkQApp()
## Define main window class from template
path = os.path.dirname(os.path.abspath(__file__))
uiFile = os.path.join(path, 'designerExample.ui')
WindowTemplate, TemplateBaseClass = pg.Qt.loadUiType(uiFile)
print("Qt version:", QtCore.QT_VERSION_STR)
class MainWindow(TemplateBaseClass):
def __init__(self):
TemplateBaseClass.__init__(self)
self.setWindowTitle('pyqtgraph example: Qt Designer')
# Create the main window
self.ui = WindowTemplate()
self.ui.setupUi(self)
self.ui.plotBtn.clicked.connect(self.plot)
self.show()
### four channel plot window ####
self.graph = self.ui.plot
self.graph.scene().sigMouseMoved.connect(self.mouseMoved)
self.vLine = pg.InfiniteLine(angle=90, movable=False)
self.hLine = pg.InfiniteLine(angle=0, movable=False)
self.proxy = pg.SignalProxy(self.graph.scene().sigMouseMoved, rateLimit=10, slot=self.mouseMoved)
self.label = pg.LabelItem(justify='right')
self.data1 = []
self.data2 = []
self.p1 = self.graph.addPlot(row=1, col=0)
self.p2 = self.graph.addPlot(row=2, col=0)
self.vb = self.p1.vb
self.region = pg.LinearRegionItem()
def plot(self):
win = self.graph
#label = pg.LabelItem(justify='right')
win.addItem(self.label)
#p1 = win.addPlot(row=1, col=0)
#p2 = win.addPlot(row=2, col=0)
#p3 = win.addPlot(row=3, col=0)
#region = pg.LinearRegionItem()
self.region.setZValue(10)
# Add the LinearRegionItem to the ViewBox, but tell the ViewBox to exclude this
# item when doing auto-range calculations.
self.p2.addItem(self.region, ignoreBounds=True)
self.p1.setAutoVisible(y=True)
self.data1 = 10000 + 15000 * pg.gaussianFilter(np.random.random(size=10000), 10) + 3000 * np.random.random(size=10000)
self.data2 = 15000 + 15000 * pg.gaussianFilter(np.random.random(size=10000), 10) + 3000 * np.random.random(size=10000)
self.p1.plot(self.data1, pen="r")
self.p1.plot(self.data2, pen="g")
self.p2.plot(self.data1, pen="w")
def update():
self.region.setZValue(10)
minX, maxX = self.region.getRegion()
self.p1.setXRange(minX, maxX, padding=0)
self.region.sigRegionChanged.connect(update)
def updateRegion(window, viewRange):
rgn = viewRange[0]
self.region.setRegion(rgn)
self.p1.sigRangeChanged.connect(updateRegion)
self.region.setRegion([1000, 2000])
#cross hair
#vLine = pg.InfiniteLine(angle=90, movable=False)
#hLine = pg.InfiniteLine(angle=0, movable=False)
self.p1.addItem(self.vLine, ignoreBounds=True)
self.p1.addItem(self.hLine, ignoreBounds=True)
def mouseMoved(self, evt):
pos = evt[0] ## using signal proxy turns original arguments into a tuple
print("evt = " + repr(evt))
if self.p1.sceneBoundingRect().contains(pos):
mousePoint = self.vb.mapSceneToView(pos)
index = int(mousePoint.x())
if index > 0 and index < len(self.data1):
self.label.setText("<span style='font-size: 12pt'>x=%0.1f, <span style='color: red'>y1=%0.1f</span>, <span style='color: green'>y2=%0.1f</span>" % (mousePoint.x(), self.data1[index], self.data2[index]))
self.vLine.setPos(mousePoint.x())
self.hLine.setPos(mousePoint.y())
#proxy = pg.SignalProxy(p1.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)
win = MainWindow()
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()