PyQt hover event with svg image

I have been working on this for some time, and I cannot understand what I am doing wrong. I hope someone here can help.

I am trying to get guidance events to work when I am on the Svg element which is in QGraphicsScene. Here is the code I played 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.

+4
source share
1 answer

You track hover events for MySvg , but you add another QGraphicsSvgItem to the view, which is just an instance ( MySvg.image ) in MySvg . Your MySvg is not even displayed. Try it 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() 
+3
source

Source: https://habr.com/ru/post/1379575/


All Articles