Hello. I'm trying to get the coordinates of the mouse when clicked.
I get some values, but they don't correspond to the bar items on the
widget.
I'd like to know what I did wrong and what should be fixed.
Please take a look at the code that I attached.
[image: c.PNG]
--
You received this message because you are subscribed to the Google Groups
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/pyqtgraph/e27dbb11-c6d8-413e-abb2-c5a32e76f07bn%40googlegroups.com.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
from pyqtgraph.dockarea import *
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().sigMouseClicked.connect(self.mouse_clicked)
def mouse_clicked(self, mouseClickEvent):
print('clicked plot 0x{:x}, event: {}'.format(id(self), mouseClickEvent))
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 = MyPlotWidget()
self.pl2 = MyPlotWidget()
self.dock1.addWidget(self.pl1)
self.dock2.addWidget(self.pl2)
class CandlestickItem(pg.GraphicsObject):
def __init__(self, data):
pg.GraphicsObject.__init__(self)
self.data = data
self.generatePicture()
def generatePicture(self):
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
p.setPen(pg.mkPen('w'))
w = (self.data[1][0] - self.data[0][0]) / 3.
for (t, open, close, min, max) in self.data:
p.drawLine(QtCore.QPointF(t, min), QtCore.QPointF(t, max))
if open > close:
p.setBrush(pg.mkBrush('r'))
else:
p.setBrush(pg.mkBrush('g'))
p.drawRect(QtCore.QRectF(t-w, open, w*2, close-open))
p.end()
def paint(self, p, *args):
p.drawPicture(0, 0, self.picture)
def boundingRect(self):
return QtCore.QRectF(self.picture.boundingRect())
data = [ ## fields are (time, open, close, min, max).
(1., 10, 13, 5, 15),
(2., 13, 17, 9, 20),
(3., 17, 14, 11, 23),
(4., 14, 15, 5, 19),
(5., 15, 9, 8, 22),
(6., 9, 15, 8, 16),
]
item = CandlestickItem(data)
app = QtWidgets.QApplication(sys.argv)
w = AppWindow()
w.pl1.addItem(item)
w.show()
sys.exit(app.exec_())