简体   繁体   中英

Selecting points on a PlotWidget with Qt

I'm new to python/pyside/pyqtgraph and I'm kind of stuck in my program.

So, I have an numpy.ndarray representing 10000 values, and I plot it in a PlotWidget using the plot method. The result is ok but now I want to allow the user to select points of the curve so I can save the X axis of the point and use it later on.

What I would like to do is creating a QPushButton which when clicked it waits for the user to select two points on the curve by left-clicking and then save the X axis. Seems pretty simple conceptually but I don't find the good way of doing it. I would be really pleased if you could give me an example or something, I'm also open to any suggestion that deviate from this use case.

I can resume the code by this lines :

self.myWidget = pyqtgraph.PlotWidget()
self.myWidget.plot(myValues) # myValues is the numpy array
self.select2PointsButton = QtGui.QPushButton()
self.select2PointsButton.clicked.connect(self.waitForSelection)

def waitForSelection(self):
# Wait for a click left on the curve to select first point then save the X-axis
# Do it again to select the second point

Thanks, Morgan

Edit after Zet4 answer :

Thank you for your answer it helped me get started. In the end, I made a subclass of PlotWidget :

class PltWidget(pg.PlotWidget):

def __init__(self, parent=None):
    super(PltWidget, self).__init__(parent)
    self.selectionMode = False

def mousePressEvent(self, ev):
    if self.selectionMode:
        if ev.button() == QtCore.Qt.LeftButton:
            # How do I get the X axis ?
    else:
        super(PltWidget, self).mousePressEvent(ev)

Then I use it in my window, connecting the button signal with the slot changing the boolean of my PltWidget :

..... # Other attributes and connections of my Window
self.T0Button = QtGui.QPushButton()
self.graphicsLeft = PltWidget()
self.T0Button.clicked.connect(self.selectT0)

def selectT0(self):
    self.graphicsLeft.selectionMode = not(self.graphicsLeft.selectionMode)

I'll probably use your buffer strategy to command two selections from the user. However, I still need to know how do I get the X axis of the PlotWidget from where I clicked. If anyone using pyqtgraph know the answer, please let me know. Thanks.

My apologize, i'm not a pyqt expert, but your problem seems too be more conceptual than technical. You can use your QPushButton.clicked (in your code, the waitForSelection function) to change the functional state of your object (allow or disable point selection).

So you need to :

  • create a function that intercept the click on your pushButton (your waitForSelection function)
  • create a function that intercept left click on your graphical object (i'll assume you name it onLeftClick)
  • a functional state handler : a boolean is the easiest way for it ( isSelectedMode ).
  • a buffer that represent a point. ( buffer here, it can be your X-axis as you say'ed )

Your waitForSelection function will only inverse the state of isSelectedMode. It will also clear the buffer, before you don't need it anymore. pseudocode :

if isSelectedMode == true
    buffer = null;
isSelectedMode = !isSelectedMode;

The onLeftClick will do the harder of the work, see this pseudocode :

if isSelectedMode == true
    if buffer != null // No point save, so this click is the first one
        //Here you save your data in the buffer
    else
        // One point is saved so that's the second one. 
        // Do what you want with it and buffer
else
    // The selection mode is not active.

This only thing that missing is the way of getting your X-axis from your left click. I hope this can help you. Best regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM