【发布时间】:2020-02-24 04:25:39
【问题描述】:
通过在 Qt Designer 中将 Graphics View 小部件提升为 PlotWidget,我能够在 pyqtgraph 中轻松创建 ScatterPlotItem。我在上面绘制了一些随机数据,现在我想访问我单击的各个点。文档说可以连接 sigClicked(self, points) 信号,理论上应该返回光标下的点。但情况似乎并非如此,因为当我点击一个点时,无论我点击了哪个点,我都会得到相同的对象。我怀疑这个信号会返回整个 ScatterPlotItem 而不是任何特定点。
到目前为止,这是我的代码:
import sys, time
from timeit import default_timer as timer
from PyQt5 import QtGui
from PyQt5.QtCore import pyqtSlot, Qt, QPoint, QUrl, QEvent
from PyQt5.QtWidgets import *
from PyQt5 import QtMultimedia
from PyQt5.uic import loadUi
import pyqtgraph as pg
import numpy as np
class ScatterExample(QMainWindow):
def __init__(self):
# Main Loop
super(ScatterExample, self).__init__()
loadUi('<path/to/ui file>.ui', self)
self.setWindowTitle('ScatterExample')
self.scatter = pg.ScatterPlotItem(pxMode=False, pen=pg.mkPen(width=1, color='g'), symbol='t', size=1)
self.scatter.sigClicked.connect(self.onPointsClicked)
self.Scatter_Plot_View.addItem(self.scatter) # Scatter_Plot_View is the Graphics View I promoted to PlotWidget
n = 5
print('Number of points: ' + str(n))
data = np.random.normal(size=(2, n))
pos = [{'pos': data[:, i]} for i in range(n)]
now = pg.ptime.time()
self.scatter.setData(pos)
print(self.scatter.data)
def onPointsClicked(self, points):
print('Ain\'t getting individual points ', points)
points.setPen('b', width=2) # this turns EVERY point blue, not just the one clicked.
上面的打印语句打印:
Ain't getting individual points <pyqtgraph.graphicsItems.ScatterPlotItem.ScatterPlotItem object at 0x000001C36577F948>
如何获取我点击的点及其对应的属性,如x和y坐标?
【问题讨论】:
-
将
def onPointsClicked(self, points):更改为def onPointsClicked(self, obj, points): -
谢谢!有效!我还没有完全理解这些类的工作方式。
标签: pyqt python-3.6 pyqtgraph