neziy
neziy

Reputation: 103

Hover Event for a QGraphicsItem (PyQt4)

I want some small text to pop up when I have my curser over a QGraphicsItem in my QGraphicsScene. I have a class that inherits from QGraphicsItem, and this represents my graphical items in the scene.

I tried using the QGraphicsItem.hoverEnterEvent and I also set the setAcceptHoverEvents(True), but I still can't enable that hover event. I also came across an event filter method but I'm not sure where to implement it.

Should I install the event filter in the QGraphicsItem class, or the scene? I tried both and I'm still not getting the desired result. I want to be able to hover over all the items in the scene.

UPDATE: So I tried doing this but the hover event still isn't being detected.

class graphics_Object(QtGui.QGraphicsPixmapItem):
    def __init__(self, parent=None):
        super(graphics_Object, self).__init__(parent)
        pixmap = QtGui.QPixmap("item.png")
        self.graphics_pixItem = QtGui.QGraphicsPixmapItem(pixmap.scaled(40, 40, QtCore.Qt.KeepAspectRatio))
        self.graphics_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsSelectable)
        self.graphics_pixItem.setFlag(QtGui.QGraphicsPixmapItem.ItemIsMovable)
        self.graphics_pixItem.setAcceptHoverEvents(True)


    def hoverEnterEvent(self, event):
        print 'hello'

class graphicsScene(QtGui.QGraphicsScene):
    def __init__(self, parent=None):
        super(graphicsScene, self).__init__(parent)

    def mousePressEvent(self, event):
        self.graphics_item = graphics_Object()
    def mouseReleaseEvent(self, event)
        self.addItem(self.graphics_item.graphics_pixItem)
        self.graphics_item.graphics_pixItem.setPos(event.scenePos())

class Form(QtGui.QMainWindow):
    def __init__(self):
        super(Form, self).__init__()
        self.ui = uic.loadUi('form.ui')

        self.scene = graphicsScene()
        self.ui.view.setScene(self.scene)

        self.setMouseTracking(True)

Upvotes: 1

Views: 1819

Answers (1)

Prophacy
Prophacy

Reputation: 108

You are making another pixmapItem inside you graphics_Object class, graphics_Object is already a subclass of graphicsPixmapItem so I don't see a purpose for this.

Then you only add that nested pixmapItem to the scene and your hoverEnterEvent is on the graphics_Object which is never added to the scene. That is why you are not receiving hover events.

One of many solutions would be to just add your graphics object to the scene instead of the nested graphicsPixmapItem and use setPixmap() in the init of graphics_Object

class graphics_Object(QtGui.QGraphicsPixmapItem):
  def __init__(self, parent=None):
    super(graphics_Object, self).__init__(parent)
    pixmap = QtGui.QPixmap("item.png")
    self.setPixmap(pixmap.scaled(40, 40, QtCore.Qt.KeepAspectRatio))
    self.setFlag(QtGui.QGraphicsPixmapItem.ItemIsSelectable)
    self.setFlag(QtGui.QGraphicsPixmapItem.ItemIsMovable)
    self.setAcceptHoverEvents(True)

  def hoverEnterEvent(self, event):
    print('hello')


class graphicsScene(QtGui.QGraphicsScene):
  def __init__(self, parent=None):
    super(graphicsScene, self).__init__(parent)

  def mousePressEvent(self, event):
    self.graphics_item = graphics_Object()
  def mouseReleaseEvent(self, event):
    print('adding to scene')
    self.addItem(self.graphics_item)
    self.graphics_item.setPos(event.scenePos())

When you inherit (subclass) from a pyqt class or any class in python, think of the new class as a "copy" of the inherited class. It will behave the exact same way as the base class until you overwrite a method, in this case, we override the "init" and "hoverEnterEvent" methods to do our custom stuff. Everything else about a QGraphicsPixmapItem stays the same

Upvotes: 1

Related Questions