0

I'd like to use a InfiniteLine in in pyqtgraph that moves with the mouse moving. Upon left-clicking the function shall return the xposition. I am able to retrieve the x-value on left-clicking and moving the InfiniteLine with the mouse movement. However I am stucked at simply returning the x position. I'd like to wait for the left click, as I want to repeat this procedure multiple times.

def addVerticalLineAndGetXOnClick(self):
    def changePosVertLine(event):
        if self.sceneBoundingRect().contains(event):
            mousePoint = self.plotItem.vb.mapSceneToView(event)
            line.setPos(mousePoint.x())
    def onMouseClick(event):
        if event.button() == 1:
            self.removeItem(line)
            self.scene().sigMouseClicked.disconnect()
            self.setMouseTracking(False)
            mousePoint = self.plotItem.vb.mapSceneToView(event.scenePos())
            self.xPos.emit(mousePoint.x())
    penLin = mkPen(color = '#000000', width = 1)
    line = LRI(0.,pen = penLin, name= 'singleLineToGetXPos' )
    self.addItem(line)
    self.setMouseTracking(True)
    self.scene().sigMouseMoved.connect(changePosVertLine)
    self.scene().sigMouseClicked.connect(onMouseClick)
    #wait to return after mouse click and return
    """???"""

Sebus
  • 55
  • 1
  • 7
  • 1
    Instead of returning the value after the mouse click, store it as some internal variable within your class so you can access it anywhere – nathancy Sep 11 '19 at 00:15
  • Thanks, but what is the best way for the caller to notice, that the value has been set? – Sebus Sep 11 '19 at 05:27
  • You could send a signal when you set the value. – Heike Sep 11 '19 at 06:33
  • I agree with @Heike, if you're using PyQt, you can send a signal using `pyqtSignal()` – nathancy Sep 11 '19 at 19:53
  • Thanks a lot. Is there any convenient way to wait for a signal before continuing? – Sebus Sep 11 '19 at 20:09
  • If you connect a slot to a signal, the slot is called automatically every time the signal is emitted. The slot can be any function provided it has the correct input parameters for the signal it's connected to. – Heike Sep 11 '19 at 21:32

0 Answers0