PyQt: hover and click events for graphicscene elli

2019-08-10 23:19发布

I'd like to find out if a user hovers over or clicks a GraphicsScene shape object (for example, the ellipse object I commented below). I'm new to PyQt and the documentation is much better for C++ than Python, so I'm having a bit of trouble figuring this one 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 some items
        x = 0
        y = 0
        w = 45
        h = 45
        pen = QtGui.QPen(QtGui.QColor(QtCore.Qt.green))
        brush = QtGui.QBrush(pen.color().darker(150))

        # i want a mouse over and mouse click event for this 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 I can get an event to take place based on a mouse click release. Unfortunately, only the last item I create can respond to 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 some items
        x = 0
        y = 0
        w = 20
        h = 20
        pen = QtGui.QPen(QtGui.QColor(QtCore.Qt.green))
        brush = QtGui.QBrush(pen.color().darker(150))

        # i want a mouse over and mouse click event for this ellipse
        for xi in range(10):
            for 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):
        # Do your stuff here.
        pass

if ( __name__ == '__main__' ):
    app = QtGui.QApplication([])
    f = MyFrame()
    f.show()
    app.exec_()

Screen shot of only last item created being 'eventable'.

标签: python pyqt4
1条回答
唯我独甜
2楼-- · 2019-08-11 00:03

You can define your own ellipse class and replace both click and hover events:

class MyEllipse(QtGui.QGraphicsEllipseItem):

    def mouseReleaseEvent(self, event):
        # Do your stuff here.
        return QtGui.QGraphicsEllipseItem.mouseReleaseEvent(self, event)

    def hoverMoveEvent(self, event):
        # Do your stuff here.
        pass

Usage:

item = MyEllipse(x, y, w, h)
item.setAcceptHoverEvents(True)
item.setPen(pen)
item.setBrush(brush)
self.scene().addItem(item)

Hope it helps!

查看更多
登录 后发表回答