Getting the hang of mouse events for drawing purposes

33 views
Skip to first unread message

Friedrich

unread,
Sep 23, 2018, 10:44:40 AM9/23/18
to pyqtgraph

Hi all

I am currently trying/struggling with the mouse interaction... I might have just passed the correct lines in the documentation but i just can't get it to work. What I want to achieve is getting the starting positon (mouse button pressed), the moving event (hovering...) and when I am finished dragging the mouse (mouse button release).

The following is a very crude MWE... can somebody help?

#!/usr/bin/env python
import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QToolBar, QAction
import pyqtgraph as pqg


class Builder(QMainWindow):

    def __init__(self, parent=None):
       super(Builder, self).__init__(parent)
       self.setupWidget()
   
   def setupWidget(self):
       self.toolbar = Toolbar(self)
       self.addToolBar(self.toolbar)

        self.win = pqg.GraphicsWindow()
       self.canvas = self.win.addPlot(row=1, col=0)
       self.canvas.showGrid(x=True, y=True)

        self.scene = self.canvas.scene()
       self.scene.sigMouseHover.connect(self.onMouseHover)
       self.scene.sigMouseClicked.connect(self.onMouseClick)
       print(self.scene.parent)
       print(dir(self.scene))
       
       self.setCentralWidget(self.win)

    def onMouseMove(self, events):
       print("MOVE", events)
       pos = events[0]
       x = self.canvas.vb.mapSceneToView(pos).x()
       y = self.canvas.vb.mapSceneToView(pos).y()
       # print(x, y)

    def onMouseClick(self, events):
       print("LENGTH", len(self.scene.clickEvents))
       print("CLICK", events)
       evts = self.scene.clickEvents
       print(evts)
       # for ev in evts:
       #     print(ev.pos())
       #     print(ev.lastPos())

    def onMouseHover(self, events):
       print("LENGTH", len(self.scene.clickEvents))
       print("HOVER", events)
       evts = self.scene.clickEvents
       print(evts)
       # for ev in evts:
       #     print(ev.pos())
       #     print(ev.lastPos())
   


class Toolbar(QToolBar):

    def __init__(self, parent=None):
       """."""
       super(Toolbar, self).__init__(parent)
       self.setupWidget()
   
   def setupWidget(self):
       self.setMovable(True)
       self.setFloatable(True)
       self.rect = QAction("rect", checkable=True)

        self.addAction(self.rect)



if __name__ == '__main__':
   app = QApplication(sys.argv)
   app.setApplicationName("hulk_mad.py")

    main = Builder()
   main.show()

    sys.exit(app.exec_())

:-/ .. I have to confess that I am a biiiiiit frustrated
Reply all
Reply to author
Forward
0 new messages