python - PyQt: hover and click events for graphicscene ellipse -


i'd find out if user hovers on or clicks graphicsscene shape object (for example, ellipse object commented below). i'm new pyqt , documentation better c++ python, i'm having bit of trouble figuring 1 out.

from pyqt4 import qtgui, qtcore  class myframe(qtgui.qgraphicsview):     def __init__( self, parent = none ):         super(myframe, self).__init__(parent)          self.setscene(qtgui.qgraphicsscene())          # add items         x = 0         y = 0         w = 45         h = 45         pen = qtgui.qpen(qtgui.qcolor(qtcore.qt.green))         brush = qtgui.qbrush(pen.color().darker(150))          # want mouse on , mouse click event ellipse         item = self.scene().addellipse(x, y, w, h, pen, brush)           item.setflag(qtgui.qgraphicsitem.itemismovable)  if ( __name__ == '__main__' ):     app = qtgui.qapplication([])     f = myframe()     f.show()     app.exec_() 

update!!!

now can event take place based on mouse click release. unfortunately, last item create can respond events. what's going on here?

from pyqt4 import qtgui, qtcore  class myframe(qtgui.qgraphicsview):     def __init__( self, parent = none ):         super(myframe, self).__init__(parent)          self.setscene(qtgui.qgraphicsscene())          # add items         x = 0         y = 0         w = 20         h = 20         pen = qtgui.qpen(qtgui.qcolor(qtcore.qt.green))         brush = qtgui.qbrush(pen.color().darker(150))          # want mouse on , mouse click event ellipse         xi in range(10):             yi in range(10):                 item = callbackrect(x+xi*30, y+yi*30, w, h)                 item.setaccepthoverevents(true)                 item.setpen(pen)                 item.setbrush(brush)                 self.scene().additem(item)          # item = self.scene().addellipse(x, y, w, h, pen, brush)           item.setflag(qtgui.qgraphicsitem.itemismovable)  class callbackrect(qtgui.qgraphicsrectitem):     '''     rectangle call-back class.     '''      def mousereleaseevent(self, event):         # recolor on click         color = qtgui.qcolor(180, 174, 185)         brush = qtgui.qbrush(color)         qtgui.qgraphicsrectitem.setbrush(self, brush)          return qtgui.qgraphicsrectitem.mousereleaseevent(self, event)      def hovermoveevent(self, event):         # stuff here.         pass  if ( __name__ == '__main__' ):     app = qtgui.qapplication([])     f = myframe()     f.show()     app.exec_() 

screen shot of last item created being 'eventable'.

you can define own ellipse class , replace both click , hover events:

class myellipse(qtgui.qgraphicsellipseitem):      def mousereleaseevent(self, event):         # stuff here.         return qtgui.qgraphicsellipseitem.mousereleaseevent(self, event)      def hovermoveevent(self, event):         # stuff here.         pass 

usage:

item = myellipse(x, y, w, h) item.setaccepthoverevents(true) item.setpen(pen) item.setbrush(brush) self.scene().additem(item) 

hope helps!


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -