Jeff
Jeff

Reputation: 7210

pyQt Hover event with Svg image

I've been working on this for some time now and I can't figure out what I'm doing wrong. I hope someone here can help.

I'm trying to get hover events to work when I mouse over an Svg item that's in a QGraphicsScene. Here's the code that I've been playing with.

#!/usr/bin/python

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtSvg import *

class Main(QWidget):

    def __init__(self):
        super(Main, self).__init__()

        hbox = QHBoxLayout()

        self.setLayout(hbox)
        self.view = MyView(self)
        self.scene = QGraphicsScene()
        self.view.setScene(self.scene)

        hbox.addWidget(self.view)


class MyView(QGraphicsView):

    def __init__(self, parent):
        super(MyView, self).__init__(parent)
        self.parent = parent

    def mousePressEvent(self, event):
        super(MyView, self).mousePressEvent(event)
        test = MySvg()
        self.parent.scene.addItem(test.image)


class MySvg(QGraphicsSvgItem):

    def __init__(self):
        super(MySvg, self).__init__()

        self.image = QGraphicsSvgItem('ubuntu.svg')
        self.image.setFlags(QGraphicsItem.ItemIsSelectable|
                            QGraphicsItem.ItemIsMovable)

        self.setAcceptsHoverEvents(True)

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

    def hoverLeaveEvent(self, event):
        print 'Leave'

    def hoverMoveEvent(self, event):
        print 'Moving'


def runMain():

    app = QApplication(sys.argv)
    ex = Main()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    runMain()

Hope someone can help.

Upvotes: 4

Views: 2624

Answers (1)

Avaris
Avaris

Reputation: 36715

You are monitoring hover events for MySvg but you are adding another QGraphicsSvgItem to the view that is just an instance (MySvg.image) in MySvg. Your MySvg is not even in the view. Try like this:

#!/usr/bin/python

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtSvg import *

class Main(QWidget):

    def __init__(self):
        super(Main, self).__init__()

        hbox = QHBoxLayout()

        self.setLayout(hbox)
        self.view = MyView(self)
        self.scene = QGraphicsScene()
        self.view.setScene(self.scene)

        hbox.addWidget(self.view)


class MyView(QGraphicsView):

    def __init__(self, parent):
        super(MyView, self).__init__(parent)
        self.parent = parent

    def mousePressEvent(self, event):
        super(MyView, self).mousePressEvent(event)
        test = MySvg()
        self.parent.scene.addItem(test)


class MySvg(QGraphicsSvgItem):

    def __init__(self):
        super(MySvg, self).__init__('ubuntu.svg')

        self.setFlags(QGraphicsItem.ItemIsSelectable|
                      QGraphicsItem.ItemIsMovable)

        self.setAcceptsHoverEvents(True)

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

    def hoverLeaveEvent(self, event):
        print 'Leave'

    def hoverMoveEvent(self, event):
        print 'Moving'


def runMain():

    app = QApplication(sys.argv)
    ex = Main()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    runMain()

Upvotes: 4

Related Questions